So in my code I am trying to add numbers together in a loop, what I mean is that once you enter a number it adds it to the previous and gives you the new sum. I have this working fine, but when I enter a 0, which ends the program, giving you a message saying whatever the last sum was, it restarts the program, and I can't seem to make it end. Here is the website which I am using to get my work from. I am on exercise 26.
Here is my code:
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;
}
}
}
}
}
}
Any help would be much appreciated.