You are on page 1of 1

// File : introductory/AddingMachine.java // Purpose: Read and sum numbers from the console.

// Author : Fred Swartz - 2007-04-05 - Placed in public domain. package sum; import java.util.*; public class AddingMachine { public static void main(String[] args) { //... Initialization double total = 0; // Sum of all input numbers. Scanner in = new Scanner(System.in); //... Prompt and read input in a loop. System.out.println("As you enter numbers, they will be added."); System.out.println("Entering a non-number will stop the program."); while (in.hasNextDouble()) { double n = in.nextDouble(); total = total + n; System.out.println("The total is " + total); } } }

You might also like