I'm trying to learn Java, I studied Pascal in high school and it has the repeat until..;
instruction.
I want to solve an exercise where I'm supposed to keep entering numbers until the penultimate + antepenultimate numbers equal the last number I entered.(a[i-2]+a[i-1] = a[i]);
I'm doing it without arrays but that doesn't really matter.
In Pascal it would be easy because repeat until is more easier to use For ex it would be
repeat
...
until ((a[i-2]+a[i-1] = a[i]) and (n=3));
n
is the number of values I entered
I can't figure out how to introduce it in Java, so far I did this but it doesn't work if I enter 2 2 4
. It should stop but it 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
is the last number I enter, s
is the sum of the numbers entered.
Could anyone tell me how to set the conditions so it will stop if I enter the values 2 2 4
?