私のコードには、最終結果がどうなるかを説明するコメントがありますが、この質問に答えながら、その目標を達成する方法には答えないでください。
基本的に、1 に等しい "currentNum" というラベルの付いた int があります。currentNum が 400 万未満になるまで実行される while ループがあります。しかし、何らかの理由でループが実行されていません。while ループの外側はすべて実行されますが、while ループ自体は実行されません。
'HI' がコンソールに 1 回表示されます。「LOOP」はコンソールに表示されません。
コード:
/*Each new term in the Fibonacci sequence is generated by adding the previous two terms.
* By starting with 1 and 2, the first 10 terms will be:
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
* By considering the terms in the Fibonacci sequence whose values do not exceed four million,
* find the sum of the even-valued terms.*/
public class Solution {
public static void main(String args[]) {
int lastNum1 = 0;
int lastNum2 = 0;
int currentNumEven = 0;
int currentNum = 1;
int sumEven = 0;
boolean last = true;
System.out.println("HI");
while(currentNum < 4000000);
System.out.println("LOOP");
currentNum = currentNum + (lastNum1 + lastNum2);
if(last) {
lastNum1 = currentNum;
last = !last;
} else {
lastNum2 = currentNum;
last = !last;
}
if(currentNum % 2 == 0) {
currentNumEven = currentNum;
sumEven += currentNum;
System.out.println(currentNumEven);
System.out.println(currentNum);
}
if(currentNum < 4000000) {
currentNum++;
} else {
System.out.println("Sum of all even Fibonacci values: " + sumEven + "\n Last even number of sequence below 4,000,000" + currentNumEven);
}
}
}