Неограниченная игра на угадывание Java 0-100 нужно распечатать, сколько угаданий потребовалось, чтобы угадать число правильно - PullRequest
0 голосов
/ 28 января 2019

Мне нужно создать переменную Scanner или что-то еще в последнем операторе печати, когда пользователь вводит последнее число.Я попытался реализовать несколько вариантов операторов println для добавления, и ничего не получалось.Спасибо.

import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

public static void main(String[] args) {

    // Create a random number generator

    Random random = new Random();

    // Use Scanner for getting input from user

    Scanner scanner = new Scanner(System.in);

    // Use the random generator to 
    // pick a number between 0 and 99 (inclusive)

    int number = random.nextInt(10);
    int guess = -1;
    int count = 0;

    // Loop until the user has guessed the number

    while (guess != number) {

        // Prompt the user for their next guess

        System.out.print("Guess a number between 0-100: P.S. I will tell you if you are too low, or too high. Good Luck  \n  ");

        // Read the users guess

        guess = scanner.nextInt();
        count++;

        // Check if the guess is high, low or correct

        if (guess < number) {

            // Guess is too low

            System.out.println("Too low, please try again");

        } else if (guess > number) {

            // Guess is too high

            System.out.println("Too high, please try again");

        } else {

            // Guess is correct !!

            System.out.println("Correct, the number was " + number + ". You needed " + count + " times");
        }
    }
}

}

1 Ответ

0 голосов
/ 28 января 2019

Добавить переменную счетчика перед циклом while

int count = 0;

Увеличивать его после каждого нового предположения

guess = scanner.nextInt();
count++;

Печатать, когда пользователь вводит правильный ответ

System.out.println("Correct, the number was " + number + ". You needed " + count + " times");
...