Answer:
The program doesn't use comments (See explanation section for line by line explanation)
The program written in java is as follows:
import javax.swing.*;
import java.util.*;
public class QuestionNumber4
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int countValidNumber=0;
int num;
System.out.print("Enter an integer: ");
for(int i=1;i<=10;i++)
{
if(input.hasNextInt())
{
if(i!=10)
{
System.out.print("Enter an integer: ");
num = input.nextInt();
}
countValidNumber++;
}
else
{
System.out.println("input is not valid");
if(i!=10)
{
System.out.print("Enter an integer: ");
input.next();
}
}
}
System.out.println("Valid Numbers Are:"+countValidNumber);
}
}
Step-by-step explanation:
The next two lines import required libraries into the program
import javax.swing.*;
import java.util.*;
The next line is the class declaration
public class QuestionNumber4 {
The next line declares the main method of tge program
public static void main(String[] args) {
The next line enables the program to accept inputs from the user
Scanner input = new Scanner(System.in);
The next line initializes countValidNumber to 0
int countValidNumber=0;
The next line declares the variable that'll be used for user input
int num;
The next line prompts the user for input
System.out.print("Enter an integer: ");
The next line is the beginning of an iteration that allows the user up to 100 inputs
for(int i=1;i<=100;i++) {
The next line checks if user input is an integer
if(input.hasNextInt()) {
If user input is an integer, the italicized section is executed
if(i!=100) -> This checks if iteration is up to 100
{
System.out.print("Enter an integer: "); -> This line prompts the user for another input
num = input.nextInt(); -> This line accepts the input
}
countValidNumber++; -> count of valid integers in incremented by 1
}
If user input is not an integer, the underlined section is executed
else
{
System.out.println("input is not valid"); -> This lines displays a message that user input is invalid
if(i!=100) ->This checks if iteration is up to 100
{
System.out.print("Enter an integer: "); -> This line prompts the user for another input
input.next();
}
}
} -> The iteration ends here
System.out.println("Valid Numbers Are:"+countValidNumber); -> This line prints total valid inputs
} -> Main Method ends here
}-> The class ends here