Hello I need help trying to get another opinion on this question. This code snip
ID: 674904 • Letter: H
Question
Hello
I need help trying to get another opinion on this question.
This code snippet uses a “while” loop. The intent of the program is to ask the user to enter 10 numbers and only 10 numbers. What is wrong with the code and how would you fix it? How would you test your fix to verify that you fixed the problem(s)?
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numbersEntered = 0;
int number;
while (numbersEntered < 10) {
System.out.println("Please enter a number");
number = input.nextInt();
System.out.print("You have entered a total of " + numbersEntered + " ");
}
}
Explanation / Answer
import java.io.*;
import java.util.Scanner;
class ReadAndPrintTenNumbers
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numbersEntered = 0;
int number;
while (numbersEntered < 10) {
numbersEntered++; //This will maintain the counter for numbers entered.
System.out.print("Please enter a number "+numbersEntered+": "); //This will prompt you to enter ith number where i lies between 1 and 10.
number = input.nextInt();
}
System.out.print("You have entered a total of " + numbersEntered + " "); //This will ensure the total numbers (10) entered.
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.