In JAVA, answer the following: 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.

1 answer
Question:

in JAVA, answer the following:
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.

Answers

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..."); }}OUTPUTEnter any number: 0Enter any number: 23Enter any number: 10Number 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.

Similar Solved Questions

1 answer

A Morrison’s fuel truck is traveling at a speed of 5 miles per hour for 5 minutes what distance does it travel?

A Morrison’s fuel truck is traveling at a speed of 5 miles per hour for 5 minutes what distance does it travel?...
1 answer

Six hundred and eight thousandths as a decimal

Six hundred and eight thousandths as a decimal...
2 answers

Who made up the workforce? What were their working conditions?

Who made up the workforce? What were their working conditions?...
1 answer

Please help me solve the below question on the attached picture?

Please help me solve the below question on the attached picture?...
1 answer

What was the expected British reaction to the Declaration of Independence

What was the expected British reaction to the Declaration of Independence...
1 answer

2 square root of 3 (square root of 2 plus square root of 3 )

2 square root of 3 (square root of 2 plus square root of 3 )...
2 answers

Austin buy 4 boxes of nails for his project. there are 30 nails in each box. how many nails does Austin Buy in all

Austin buy 4 boxes of nails for his project. there are 30 nails in each box. how many nails does Austin Buy in all...
2 answers

What is the purpose of the locks

what is the purpose of the locks...
1 answer

Calculate your pay if you work h hours in one week and are Paid $10 per hour

Calculate your pay if you work h hours in one week and are Paid $10 per hour...
2 answers

Which one of the following substances produces ammonia gas when heated with ammonium chloride solution? a. Sodium oxide b. Sulphur dioxide c. Hydrochloric acid d.Sodium hydroxide

Which one of the following substances produces ammonia gas when heated with ammonium chloride solution? a. Sodium oxide b. Sulphur dioxide c. Hydrochloric acid d.Sodium hydroxide...
1 answer

Rw/6+y=f And I have to solve for y so I’m confused on how to isolate it.

rw/6+y=f And I have to solve for y so I’m confused on how to isolate it....
1 answer

3x=6x+9. -2x+y=3. write system in slope- intercept form​

3x=6x+9. -2x+y=3. write system in slope- intercept form​...
1 answer

What did the Chinese use as a wall before they built the great wall

what did the Chinese use as a wall before they built the great wall...
1 answer

How do most Palestinians view the Jewish settlements in Israel

how do most Palestinians view the Jewish settlements in Israel...
2 answers

Answer this question ASAP please. It’s a short answer open ended question.

Answer this question ASAP please. It’s a short answer open ended question....
1 answer

2. What is the difference between an element and a compound?​

2. What is the difference between an element and a compound?​...
1 answer

PLEASE HELPPP 10 points and will mark brainly!!!!!!

PLEASE HELPPP 10 points and will mark brainly!!!!!!...
1 answer

An electron moves through a uniform electric field E = (2.00î + 5.40ĵ) V/m and a uniform magnetic field B = 0.400k T. Determine the acceleration of the electron when it has a velocity v = 8.0î m/s. (Give each component in m/s2.)

An electron moves through a uniform electric field E = (2.00î + 5.40ĵ) V/m and a uniform magnetic field B = 0.400k T. Determine the acceleration of the electron when it has a velocity v = 8.0î m/s. (Give each component in m/s2.)...

-- 0.048178--