2017-08-31 28 views
2

所以在我的代碼,我想在一個循環加在一起的數字,我的意思是,一旦你輸入一個數字它把它添加到以前併爲您提供新的總和。我有這個工作正常,但是當我輸入一個0,結束程序,給你一個消息,說無論最後一筆是,將重新啓動該程序,我似乎無法讓它結束。這裏是我用來獲得我的作品的網站from。我在練習26的Java雖然真實的說明重複時,它應該已經打破了

這裏是我的代碼:

package July28Restart; 

import java.util.Scanner; 

public class Ex26SumOfManyNumbers { 
    public static void main(String args[]){ 
     Scanner reader = new Scanner(System.in); 

     int sum = 0; 
     while (true){ 

      System.out.println("Enter numbers one by one, and continue entering them. The system will continue adding them together with the last sum. When you enter 0, the last sum will be the last."); 
      int read = Integer.parseInt(reader.nextLine()); 

      if (read == 0){ 
       break; 
      } else if (read != 0){ 
       while (true){ 
        sum = sum + read; 
        System.out.println("Sum now: " +sum); 
        read = Integer.parseInt(reader.nextLine()); 
        if (read == 0) { 
         System.out.println("The sum in the end was: "+sum); 
         break;    
        } 
       } 
      } 
     } 
    } 
} 

任何幫助將非常感激。

+0

您是否嘗試過使用調試器通過該代碼? https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – litelite

+0

沒有,我不知道該怎麼做,我新來的編程 –

+1

@JayMueller停止,並學習如何使用IDE調試器。這是您必須具備的基本技能,才能進行任何編程。 –

回答

2

你可以使用標籤來從內環內突破外循環:

outer: 
while (true){ 
    // ... 
    while (true){ 
     // ... 
     if (read == 0) { 
      System.out.println("The sum in the end was: "+sum); 
      break outer;    
     } 
    } 
    // ... 
} 
2

因爲如果你進入另一個號碼後進入0您從第二循環突破至第一,且僅當您(後直接另一個)進入0兩次會退出

更正代碼:

package July28Restart; 
import java.util.Scanner; 

public class Ex26SumOfManyNumbers { 
    public static void main(String args[]) { 
     Scanner reader = new Scanner(System.in); 

     int sum = 0; 
     while (true) { 

      System.out.println(
        "Enter numbers one by one, and continue entering them. The system will continue adding them together with the last sum. When you enter 0, the last sum will be the last."); 
      int read = Integer.parseInt(reader.nextLine()); 

      if (read == 0) { 
       exitProgram(sum); 
      } else if (read != 0) { 
       while (true) { 
        sum = sum + read; 
        System.out.println("Sum now: " + sum); 
        read = Integer.parseInt(reader.nextLine()); 
        if (read == 0) { 
         exitProgram(sum); 
        } 

       } 

      } 

     } 

    } 

    private static void exitProgram(int sum) { 
     System.out.println("The sum in the end was: " + sum); 
     System.exit(0); 

    } 

} 
+1

不要使用'System.exit' - 它把一個嚴重的限制,在可重用性和代碼的擴展。另外,不要使用'boolean'值來控制你的循環,這就是標籤的作用:你只需在外層循環之前添加'outer:'並將你的'return;'變爲'return outer;'。工作只需一行額外的代碼。 –

+0

我不想優化代碼,所以TO會識別他的代碼並查看代碼更改。但對於真實的項目,您的評論絕對有效且有幫助。 –

2

對不起,剛纔看到你的問題。正如Jaroslaw Pawlak解釋的那樣,你可以在這裏使用外層,或者只能重構和使用一層。東西看起來像:

public class Ex26SumOfManyNumbers { 
    public static void main(String args[]){ 
     Scanner reader = new Scanner(System.in); 
     calSum(reader); 

    } 

    public static void calSum(Scanner reader) { 
     System.out.println("Enter numbers one by one, and continue entering them. The system will continue adding them together with the last sum. When you enter 0, the last sum will be the last."); 
     int sum = 0; 
     while (true){ 
      int read = Integer.parseInt(reader.nextLine()); 
      if(read != 0) { 
       sum = sum + read; 
       System.out.println("Sum now: " +sum); 
      } else { 
       break; 
      } 
     } 
     System.out.println("The sum in the end was: " + sum); 
    } 
} 
相關問題