Hello. Purpose: a program that generates two random numbers from a specified range, compares them. If not equal, displays the message: "Not equal, attempt No.-X" and suggests new integers. This will end when Random ints match.

public class GonaNumber{ public static void main(String[]args){ String stars = (************); int numGuess = 0;// счетчик попыток //первое сравниваемое число int randomA = (int)(Math.random()*10+1); //второе сравниваемое число int randomB = (int)(Math.random()*10+1); System.out.println(stars); System.out.println("Загадываем числа для сравнения"); System.out.println(stars); numGuess++; while(randomA != randomB){ System.out.println("Хм не в этот раз... Попытка номер "+numGuess); numGuess++ } System.out.println("Совпали на "+numGuess+" попытке"); } 

I stopped the computer in the second minute of the launch of the program, then it dawned that I compared the same numbers five million times.

  • one
    And what is required then? to rewrite your existing code to work? - Victor
  • Yes, that's my goal. I apologize for the question not correctly asked. "New" that take me)) - CoffeJava
  • one
    no problems. just add a question just in case :) - Victor

2 answers 2

You compare the same numbers, randomly in each iteration of the loop. This should work.

 public class GonaNumber{ final Random random = new Random(); public static void main(String[]args){ String stars = (************); int numGuess = 1;// счетчик попыток System.out.println(stars); System.out.println("Загадываем числа для сравнения"); System.out.println(stars); int randomA = 0; int randomB = 0; while(true){ //первое сравниваемое число randomA = random.nextInt(); //второе сравниваемое число randomB = random.nextInt(); if(randomA!=randomB){ System.out.println("Хм не в этот раз... Попытка номер "+numGuess); numGuess++; }else{ break; } } System.out.println("Совпали на "+numGuess+" попытке"); } 

And you do not worry about creating an integer from a real one. you have a Random \ ThreadLocalRandom and a wonderful .nextInt (). no castes, no * 10 + 1, it is a pleasure.

  • Does not work. It requires to declare variables even before while, and then swears that the variables are repeated: -Duplicate local variable - CoffeJava
  • @CoffeJava updated. wake up some nonsense that you sent, do not be angry. - Victor
  • I'm not angry. Fit your own under your code, as a result, the program started but ... I understand that there is no limit to the numbers? Already 11 million times compared and all by. - CoffeJava
  • @CoffeJava, in theory, we get a random number within the int, and this, if memory serves me from -2147483648 to 2147483647, i.e. the chance of coincidence is not too big, for the purity of the experiment, try to do nextInt (20), so you will get numbers from 0 to 20. It should appear faster. - Victor
  • one
    Gut Victor, thanks, earned. Match at 7, 20, 41 attempts. - CoffeJava

Add to the body of the while :

 randomA = (int)(Math.random()*10+1); randomB = (int)(Math.random()*10+1)