所以在我的代碼,我想在一個循環加在一起的數字,我的意思是,一旦你輸入一個數字它把它添加到以前併爲您提供新的總和。我有這個工作正常,但是當我輸入一個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;
}
}
}
}
}
}
任何幫助將非常感激。
您是否嘗試過使用調試器通過該代碼? https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – litelite
沒有,我不知道該怎麼做,我新來的編程 –
@JayMueller停止,並學習如何使用IDE調試器。這是您必須具備的基本技能,才能進行任何編程。 –