In: Computer Science
Create an application that makes the user guess a number. The user must be allowed tries. You must have a loop, user input (Scanner), and a constructor. (JAVA)
/*If you any query do comment in the comment section else like the solution*/
import java.util.Scanner;
public class GuessNumber {
public static void main(String arg[]) {
Scanner sc = new Scanner(System.in);
int number = 1 + (int) (10 * Math.random());
int trials = 3;
int i, guess;
System.out.println("A number is chosen between 1 to 10. Guess the number within " + trials + " trials");
for (i = 0; i < trials; i++) {
System.out.println("Enter your guess: ");
guess = sc.nextInt();
if (number == guess) {
System.out.println("Correct Answer");
break;
}
}
if (i == trials) {
System.out.println("You have exhausted " + trials + " trials.");
System.out.println("The number was " + number);
}
}
}