Ask Question

java 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.

+1
Answers (1)
  1. 28 May, 13:20
    0
    import java. util. Scanner;

    public class num8 {

    public static void main (String[] args) {

    Scanner in = new Scanner (System. in);

    int n = 0;

    for (int i = 1; i<=10; i++) {

    System. out. println ("Enter the values");

    n + = in. nextInt ();

    }

    }

    }

    Explanation:

    A complete Java program implementing the solution is given above. The Scanner class is imported to allow for user input The variable n is declared and initialized to 0. Using a for loop, the user is repeatedly asked to enter a value The value is added to the variable n with this statement n + = in. nextInt (); The for loop condition for (int i = 1; i<=10; i++) ensures that it runs only ten times.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “java 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 ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers