的Java虽然真实的说明重复时,它应该已经打破了

问题描述:

所以在我的代码,我想在一个循环加在一起的数字,我的意思是,一旦你输入一个数字它把它添加到以前并为您提供新的总和。我有这个工作正常,但是当我输入一个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://*.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – litelite

+0

没有,我不知道该怎么做,我新来的编程 –

+1

@JayMueller停止,并学习如何使用IDE调试器。这是您必须具备的基本技能,才能进行任何编程。 –

你可以使用标签来从内环内突破外循环:

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

因为如果你进入另一个号码后进入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会识别他的代码并查看代码更改。但对于真实的项目,您的评论绝对有效且有帮助。 –

对不起,刚才看到你的问题。正如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); 
    } 
}