mardi 7 mai 2019

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

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?




Aucun commentaire:

Enregistrer un commentaire