do while loop
2. Write a java program that reads in a list of int values one per line
and outputs their sum as well as the numbers read with each number.
Your programs will ask the user how many integers there will be, and
fill the table with the integers input.
Sample output:
How many numbers will you enter?
2
Enter 2 integers one per line:
20
30
The sum is 50
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("How many numbers will you enter?");
int numbers = in.nextInt();
System.out.println("Enter " + numbers + " integers one per line:");
int sum = 0;
do {
int number = in.nextInt();
sum += number;
System.out.println(number);
numbers--;
} while (numbers > 0);
System.out.println("The sum is " + sum);
}
}
Comments
Leave a comment