Write the main method of a class that asks the user to enter numbers until the s
ID: 3916655 • Letter: W
Question
Write the main method of a class that asks the user to enter numbers until the sum of all of the numbers
that are entered is exactly 10. The program will then display how many numbers were entered.
Sample Data #1:
Enter a number: 1.5
Enter a number: 3.5
Enter a number: 5
3
Sample Data #2:
Enter a number: 20
Enter a number: -12.25
Enter a number: 0
Enter a number: 2.25
4
Sample Data #3:
Enter a number: 9.1
Enter a number: -1.5
Enter a number: 1
Enter a number: -10
Enter a number: 0
Enter a number: 10
Enter a number: 1.4
7
Explanation / Answer
import java.util.Scanner; public class SumTo10 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int count = 0; double total = 0; while (total != 10) { System.out.print("Enter a number: "); total += in.nextDouble(); count++; } System.out.println(count); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.