7.9k views
2 votes
Integer userIn is read from input. Write a while loop that reads integers from input until the integer 10 is read. Then, find the sum of all integers read. Integer 10 should not be included in the sum.

Ex: If the input is 3 9 -23 -40 10, then the output is:
-51

import java.util.Scanner;

public class SumCalculator {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int userIn;
int result;

result = 0;
userIn = scnr.nextInt();

/* Your code goes here */

System.out.println(result);
}
}

1 Answer

1 vote

Answer:

Here's the modified code that reads integers from input until the integer 10 is read and then finds the sum of all integers read, excluding 10:

Step-by-step explanation:

import java.util.Scanner;

public class SumCalculator {

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

int userIn;

int result = 0;

userIn = scnr.nextInt();

while (userIn != 10) {

result += userIn;

userIn = scnr.nextInt();

}

System.out.println(result);

}

}

User Zod
by
8.7k points