jeudi 19 mars 2020

How to show the number of attempts and 1 count only for the same number that input

Help! it is guessing game and i have a problem, it need to print the number of attempts but only one count to the same number multiple time consecutively that the user input.

public static void main(String[] args) { 
    // TODO code application logic here

int ans,guess;
final int max = 10;


Scanner jai = new Scanner(System.in);
Random no = new Random();

int att = 0;
ans = no.nextInt(max) + 1;

while (true){
    System.out.println("Enter a number between 1 to 10");
    guess = jai.nextInt();
    att += 1;
    att;

if (guess == ans){
    System.out.println("You are correct, the answer is :"+ ans);
    System.out.println(att +" attempts to find the correct number!");
     System.exit(0);
}else if (guess > ans){
    System.out.println("Too Large");
}else if (guess < ans){
    System.out.println("Too Small"); 

ex:it need to count the 5 once Enter a number between 1 to 10 5 Too Small Enter a number between 1 to 10 5 Too Small Enter a number between 1 to 10 8 Too Small Enter a number between 1 to 10 9 Too Small Enter a number between 1 to 10 10 You are correct, the answer is :10 5 attempts to find the correct number!




Aucun commentaire:

Enregistrer un commentaire