ループ中にランダムにランダムに置くために選択された番号を、ロールまで

Sukonbu :

ループしながら、練習しながら、私はあなたがそれをロールバックするのにかかるどのように多くの試行乱数と、あなたの推測を入れ、コードを作ってみましたが、私はしばらくの内部で変数「チャンス」を宣言カントが、私はそれの前にそれを置けばそれだけで1つの番号を転がり続けます。

    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.");
}

それはwhileループに入ったので、どのように私は、int型の機会を宣言することができますか?

コリンはルノー:

あなただけの再割り当てることができる機会をwhileループ内の新しい値に:

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

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=119897&siteId=1