我在做一個小程序,並且遇到了問題。如果循環中的數據與第一個循環中的數據不同,則Java輸出
事情是我想確認(在這種情況下循環通過2次)輸入的數據是相同的兩次。如果它是錯誤的,它應該在最後輸出它不正確。
int yearInput;
int monthInput;
int i = 2;
int x = 1;
int inputvalid;
int same = 0;
while (x == 1) {
while (i >= 1) {
System.out.println(i);
i--;
System.out.println("enter year");
yearInput = kb.nextInt();
System.out.println("enter month");
monthInput = kb.nextInt();
}
x--;
}
same = ???;
if(same==0){
System.out.print("Both loop inputs are same");
}
else{
System.out.println("Both loop inputs are not same");
}
}
雖然這個計劃只是爲了說明問題,x和i的值是在真正的程序變量,我只是無法通過該段去。
我想添加一個計數器以某種方式附加到輸入? –
您可以將數據保存在某處在一個數組中。 – SpiderPig
爲什麼要用while循環代替if語句?現在你只有兩個無限循環,因爲'x'和'i'永遠不會改變。 –