2015-08-08 112 views
1

這是我的代碼爲什麼我的循環只運行一次?

executeProgram: 
    for (int i= 0; 1< memory.length; i++) 
    { 
     String twoDigitMemory = String.format("%02d",memory[i]); 
     System.out.print(twoDigitMemory +" ? +"); 
     accumulator = input.nextInt(); 

     if (input.nextInt() == -99999) 
     { 
      System.out.println("*** Program loading completed ***"); 
      System.out.println("*** Program execution begins ***"); 
      break executeProgram; 
     } 
    } 

這是我的輸出。我進入123

***   Welcome to Simpletron!   *** 
*** Please enter your program one instruction *** 
*** (or data word) at a time. I will display  *** 
*** the location number and a question mark (?) *** 
*** You then type the word for that location. *** 
*** Type -99999 to stop entering your program *** 
00 ? +123

進入123之後,按進入我希望在for循環再次運行 印刷「00?+」,但沒有任何反應,輸入也得到保存到變量

+2

你實際上意味着寫'1

+2

你的if應該是'if(accumulator == -99999)'? – SMA

+1

你有一個錯字。我懷疑你的for循環應該是:'for(int i = 0; i scrappedcola

回答

2

你是期待用戶輸入兩次號碼。一旦你存儲在累加器中,而另一個只是比較不需要的數字,如if (input.nextInt() == -99999)。相反,你應該使用accumulater變量檢查,如:

if (accumulater == -99999)