Simple at first glance, the task does not give rest.

import java.util.Random; import java.util.Scanner; public class First { public static void main(String[] args) { go_ahead(); } private static void go_ahead() { //запускаем рандом final Random random = new Random(); System.out.println(strings_stock.hello); //запускаем сканер Scanner sc = new Scanner(System.in); //принимаем переменную от пользователя int i = sc.nextInt(); //проверка должна будет произойти не меньше одного раза int numGuess = 1; //закрываю сканер sc.close(); while (true) { //введенное число это "потолок" для рандома int randomA = random.nextInt(i); int randomB = random.nextInt(i); //начинаю сравнивать if (randomA != randomB) { System.out.println(); //объявляю о неудаче, количество попыток, загаданные цифры System.out.println(strings_stock.ups + strings_stock.numGuess + numGuess + strings_stock.tru); System.out.println(strings_stock.myRandNums + randomA + strings_stock.and + randomB); numGuess++; //если совпали выходим из цикла } else { break; } } //объявлю удачный ход и количество попыток System.out.println(); System.out.println(strings_stock.end + " на " + numGuess + strings_stock.tru); //проблема! не выдаёт значение совпавших чисел, ругается на randomA System.out.println(strings_stock.myRandNum + randomA); } } 

If I declare a variable before the loop, the compiler is unhappy that it is duplicated. How to show matched numbers?

    1 answer 1

    The scope of variables (not counting class fields) is limited to a block of code in which they are declared. Those. code outside {} does not see variables declared inside {}

    Thus, you need to declare a variable in the same block of code where you refer to it (that is, before the loop) or block higher (at the class level). And the duplication of variables is not visible.

    • If before the cycle write int randomA; and leave the loop int randomA = random.nextInt(i); then the error will be. Presumably, the author of this. - Regent
    • @Regent, it is possible) in this case, you just need to erase the int inside the loop) - YuriiSPb
    • 2
      @CoffeJava is not, should not offer. Review my comment again: the loop should be randomA = random.nextInt(i); , not int randomA = random.nextInt(i); . - Regent
    • one
      @CoffeJava, well, type something like this: int randomA = random.nextInt(i); while (true) { int randomA = random.nextInt(i); if (randomA != randomB) { } else { break; } } //System.out.println(strings_stock.myRandNum + randomA); int randomA = random.nextInt(i); while (true) { int randomA = random.nextInt(i); if (randomA != randomB) { } else { break; } } //System.out.println(strings_stock.myRandNum + randomA); - JuriySPb
    • 3
      @CoffeJava when you write int randomA = 0 you declare a new variable of type int with a value of 0, when just randomA = 0 you assign a value to an existing variable 0. Each block of code can contain variables with the same name and not interfere with each other. Thus, in the first case, you have two different variables with the same name; in the second, the same variable. Read more about the scope of variables, their initialization and naming. - pavlofff