While loop until you randomly roll selected number, where to put random

Sukonbu :

while practicing while loops I tried to make code where you put random number and then your guess how many tries it takes to roll it but I cant declare variable "chance" inside the while but if I put it before it it just keep rolling 1 number.

    Random rng = new Random();
    Scanner input = new Scanner(System.in);
    System.out.println("Select a number you want to roll");
    int choice = input.nextInt();
    System.out.println("You feelin' lucky?\nHow many tries until you get " + choice);
    int tries = input.nextInt();
    int count = 0;
    int chance = rng.nextInt((100)+1);
        while (choice != chance) {
            System.out.println(chance);
            count++;

    }
    System.out.println("You won! It only took " + count + " tries.");
}

How can i declare the int chance so it gets into while loop?

colin renaud :

You can just re-assign chance to a new value in the while loop :

int count = 0;
int chance = rng.nextInt((100)+1);
while (choice != chance) {
    System.out.println(chance);
    chance = rng.nextInt((100)+1);
    count++;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=116229&siteId=1