169k views
2 votes
Given an int variable n that has already been declared, write some code that repeatedly reads a value into n, until at last a number between 1 and 10 (inclusive) has been entered. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.

User Jchu
by
6.5k points

1 Answer

4 votes

Answer:

Hi!

var int n = 0; // Initialize the variable n;

stdin = new Scanner(System.in); // Instanciate the Scanner object.

n = stdin.nextInt(); // It saves the first input before goes on the while loop.

while( (n<1) && (n>10) ) {

n = stdin.nextInt(); // Keeps looping until a number between 1 and 10 (inclusive) has been entered.

}

User Pbond
by
5.9k points