Sum numbers Learn programming Java

Lesson:

Flow Control


Exercise:

Sum numbers 13


Objetive:

Write a java program to ask the user for an undetermined amount of numbers (until 0 is entered) and display their sum, as follows:

Number? 5
Total = 5
Number? 10
Total = 15
Number? -2
Total = 13
Number? 0
Finished


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int number;
		int sum = 0;

		do
		{
			System.out.print("Number: ");
			number = Integer.parseInt(new Scanner(System.in).nextLine());

			sum = number + sum;

			if (number != 0)
			{
				System.out.printf("Total: " + sum + "\r\n");
			}
		} while (number != 0);

		System.out.println("Finished");
	}
}