0
我在編寫循環的簡單任務時遇到了問題。我需要爲循環做如下:for循環輸出
1)提示用戶,詢問什麼是50 + 10 =
2)如果用戶輸入錯誤答案的警告信息會彈出說,他們有2個更多的嘗試
3)一旦用戶耗盡了他們所有的嘗試不同的信息會彈出說你有沒有更多的嘗試
這是我已經能夠拿出:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int attempt = 1;
int answer = 60;
for(attempt = 1; attempt < 0; --attempt)
System.out.print(" 50 + 10 = ");
answer = input.nextInt();
input.nextLine();
if(answer != 60)
{
System.out.printf("Invalid! Try Again! %d attempt(s) left! ", attempt);
System.out.print("\n50 + 10 = ");
answer = input.nextInt();
}
if(attempt == 0)
{
System.out.print("Sorry! You have no more attempts left!");
}
System.exit(0);
}
如果我改變從1中的控制變量的值說2它會打印50 + 10 = 50 + 10 =
當我運行該程序,它會與0嘗試輸出左而不是2次的嘗試,然後1,然後抱歉消息。
該做的工作,但我有關於控制變量的問題。當我將其更改爲2或3時,爲什麼它會爲我提示的號碼複製我放入的任何號碼? 因此,例如在我的代碼中,2會使50 + 10 = 50 + 10 = – Abweichung 2012-03-08 06:38:21
您的意思是在你的代碼中? – 2012-03-08 06:40:25
因爲你的for循環永遠不會打開.....沒有大括號({})來包含循環將執行的語句....因此它只執行第一個語句...... – 2012-03-08 06:42:10