I try to learn Java, studied Pascal in high school and had instruction repeat until..;.
I want to solve an exercise in which I have to continue to enter numbers until the penultimate + antepthultative numbers coincide with the last number I entered. (a[i-2]+a[i-1] = a[i]);I do this without arrays, but that doesn't really matter.
In Pascal it would be easy, because repeating until it is easier to use For ex it would be
repeat
...
until ((a[i-2]+a[i-1] = a[i]) and (n=3));
n - number of entered values
I cannot figure out how to present it in Java while I did this, but it does not work if I enter 2 2 4. He has to stop, but he keeps asking for numbers
int pen = 0, ant = 0, s = 0, n = 1;
int ult = input.nextInt();
s = s + ult;
do {
do {
ant = pen;
pen = ult;
ult = input.nextInt();
n++;
s = s + ult;
} while (ant + pen != ult);
System.out.println(n);
} while ((n == 3) || (n < 4));
ult- This is the last number I entered s- the sum of the numbers entered.
- , , , 2 2 4?