如何让用户多次尝试?

5kgi1eie  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(326)
import java.util.Scanner;
import java.util.Random;

/*
 *  1) the user can attempt many times and you need to display the number of successful attempt
 *  2) the range of random number 1..49
 *  3) output >> You successfully guess the number in 16 attempts
 *  4) output >> Do you want to play again?
 * */

public class GuessingGame {

    public static void main(String[] args) {
        Scanner uInput = new Scanner(System.in);
        Random randNum = new Random();
        int guessNumber, number, count=0;
        String in;
        char again; 

        System.out.println("Welcome to Guessing Game");
        do {
                number = randNum.nextInt(50); // random number in the range of 1..50

                for(int i=0; i<5; i++)
                {
                    System.out.println("Enter a number to guess: ");
                    guessNumber = uInput.nextInt(); // get guess number from user

                    if(guessNumber > number)
                    {
                        System.out.println("Too big");
                    }else if(guessNumber < number)
                    {
                        System.out.println("Too small");
                    }else
                    {
                        System.out.println("Success");
                        count+=1;
                        return;
                    }
                }
                System.out.println("You successfully guess the number in "+count);
                System.out.println("Do you want to play again? ");
                in = uInput.nextLine();

                again = in.charAt(0); //again will hold the first character from in var

        }while(again =='Y'|| again =='y');
        System.out.println("Guessing game terminate, thank you");
    }
}
vm0i2vca

vm0i2vca1#

public static void main(String[] args) {
    System.out.println("Welcome to Guessing Game");
    guessNumber();
    System.out.println("Guessing game terminate, thank you");
}

private static void guessNumber() {
    Scanner uInput = new Scanner(System.in);
    Random randNum = new Random();
    int guessNumber, number, count = 0;
    String in;
    char again;
    boolean isCorrect = false;

    number = randNum.nextInt(50); // random number in the range of 1..50

    while (!isCorrect) {
        System.out.println("Enter a number to guess: ");
        guessNumber = uInput.nextInt(); // get guess number from user

        count += 1;

        if (guessNumber > number) {
            System.out.println("Too big");
        } else if (guessNumber < number) {
            System.out.println("Too small");
        } else {
            System.out.println("Success");
            isCorrect = true;
        }
    }

    System.out.println("You successfully guess the number in " + count + " attempts");
    System.out.println("Do you want to play again? yes/no");
    in = uInput.next();

    again = in.charAt(0); //again will hold the first character from in var
    if (again == 'Y' || again == 'y') {
        guessNumber();
    }
}
deyfvvtc

deyfvvtc2#

您所要做的就是用while循环替换do while循环。但关键是,您必须将初始值“y”设置为您的字符以启动while循环。循环的条件将是相同的。代码如下:char again='y';

while (again == 'Y' || again == 'y') {
        System.out.println("Welcome to Guessing Game");
        number = randNum.nextInt(50);

        for (int i = 0; i < 5; i++) {
            System.out.println("Enter a number to guess: ");
            guessNumber = uInput.nextInt();

            if (guessNumber > number) {
                System.out.println("Too big");
            } else if (guessNumber < number) {
                System.out.println("Too small");
            } else {
                System.out.println("Success");
                count += 1;
                break;
            }
        }

        System.out.println("You have successfully guessed the number for " + count + " times");
        System.out.println("Do you want to play again? ");
        in = uInput.next();

        again = in.charAt(0); //again will hold the first character from in var
    }

    System.out.println("Guessing game terminate, thank you");

我必须注意到你的 count 变量,包含用户成功猜到的游戏数,而不是单个游戏中的尝试次数。如果你想处理这个问题,创建 attempts while循环中的变量,并在用户尝试时增加它。我也换了线 in = uInput.nextLine();in = uInput.next(); 因为我相信你的扫描仪输入会被跳过。

相关问题