Answer:
The JAVA program is as follows.
import java.util.Scanner;
public class Program
{
static int n;
public static void main(String[] args) {
//scanner object
Scanner stdin = new Scanner(System.in);
//loop executes till number entered is in the range of 1 to 10
do
{
System.out.print("Enter any number: ");
n = stdin.nextInt();
}while(n<1 || n>10);
System.out.println("Number is valid. Exiting...");
}
}
OUTPUT
Enter any number: 0
Enter any number: 23
Enter any number: 10
Number is valid. Exiting...
Explanation:
The program is explained.
1. The integer variable, n, is declared static since the variable should be accessible inside main() which is static.
2. An object of Scanner, stdin, is created inside main().
3. Inside do-while loop, the user is asked input any value for n.
4. The loop continues till user inputs a number which is not in the given range, beginning from 1 to 10.
5. The loop will not be terminated till the user enters a valid input.
6. Once a valid input is entered, the message is displayed to the console and the program ends.
7. The variable, n, is declared at the class level and is a class variable. Class variables are declared inside the class but outside all the methods in that class.
8. Since JAVA is a purely object-oriented language, all the code is written inside class.
9. The object of class is not created since only one class is included in the program. If more than one class is included, then the object of the class which does not has the main() method will be created inside main().
10. User input for integer is taken via scanner object and using nextInt() method.
11. The methods, print() and println() display the messages to the console. The only difference is that println() inserts a new line after displaying the message.
12. The name of the class having main() and the name of the program should be the same.