185k views
1 vote
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 Answer

3 votes

Answer:

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();

}

}

}

Step-by-step 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.

User Borja Tarraso
by
8.1k points