0

/你好,我正在尝试学习如何在 java 中使用“break”命令以及使用“y 或 n”选项继续循环。我正在写这个猜数游戏,我在选择“y”时遇到了一些麻烦。我会试着解释一下,写一个猜数字的游戏很容易,所以我开始添加一些条件,比如是否再次玩的可能性,后来我想如果我添加退出的可能性会更有趣时间玩家希望,但这不能正常工作。请帮忙,这是我的代码

package guessinggame;
import java.util.Random;
import java.util.Scanner;

/**
*

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    System.out.println(" Welcome ");

    Random rand = new Random();
    Scanner input = new Scanner(System.in);

    boolean play_again = true;
    while (play_again)
    {
    int number_guess = rand.nextInt(100)+1;
    int number_of_tries = 0;
    int guess;
    String another = "y";

    boolean win = false;
    while (win == false)
    {
        System.out.println(" Try too guess a number between 1 and 100 ");
        guess = input.nextInt();
        number_of_tries++;

        if (guess == number_guess)
        {
            win = true;
        }
        else if (guess < number_guess)
        {
            System.out.println(" Guess is too low " + "\n Guess another number to continue or n to quit ");
            if (input.hasNext("n"))
            {
                play_again = false;
                break;

            }
        }
        else if (guess > number_guess)
        {
            System.out.println(" Guess is too high " + "\n Guess another number to continue or n to quit ");
            if (input.hasNext("n"))
            {
                play_again = false;
                break;
            }
        }
    }

    System.out.println(" You Win!!! ");
    System.out.println(" The number was " + number_guess);
    System.out.println(" It took you " + number_of_tries + " tries " +
            "\nWould you like to play again? (y/n): ");
    if (another.equalsIgnoreCase("y") == true)
        play_again = true;
    else 
    {
        play_again = false;
        }
    }
    }
}
4

4 回答 4

2

这是实现相同结果的替代方案

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{
    System.out.println(" Welcome ");

    Random rand = new Random();
    Scanner input = new Scanner(System.in);

    int guess = 0;
    int number_of_tries = 0;

    System.out.println(" Try too guess a number between 1 and 100 -1 to close");
    guess = input.nextInt(); //get first input
    while (guess != -1)
    {
        int number_guess = rand.nextInt(5) + 1;
        ++number_of_tries;

        //check if user wins and exits loop 
        if (isWin (number_guess,guess))
        {
            System.out.println(" You Win!!! ");
            System.out.println(" The number was " + number_guess);
            System.out.println(" It took you " + number_of_tries + " tries " +
                    "\nWould you like to play again? [1 yes/ -1 no]: ");
            guess = input.nextInt();
            if (guess == -1)
            break;

            else
                System.out.println(" Try too guess a number between 1 and 100 -1 to close");
        }
        else if (number_guess < guess )
        {
            System.out.println(" Guess is too High " + "\n Guess another number to continue or -1 to quit ");
            guess = input.nextInt();
            continue;
        }
        else if (number_guess > guess)
        {
            System.out.println(" Guess is too low " + "\n Guess another number to continue or -1 to quit ");
            guess = input.nextInt();
            continue;
        }

    }
    System.out.println ("bye bye");
}

public static boolean isWin (int number,int guess)
{
    return (number == guess) ? true :false;
}

}

于 2013-11-04T06:20:55.083 回答
1

在此语句之后您忘记等待用户输入:

System.out.println(" It took you " + number_of_tries + " tries " +
        "\nWould you like to play again? (y/n): ");

例如,您可以尝试下一种方法:

System.out.println(" It took you " + number_of_tries + " tries " +
        "\nWould you like to play again? (y/n): ");
if (input.hasNext()) {
  if (another.equalsIgnoreCase("y")) {
    play_again = true;
    input.next();
  } else {
    play_again = false;
  }
} 
于 2013-11-04T05:47:02.440 回答
0

看起来你的牙套又被你的玩弄乱了 if 声明

于 2013-11-04T05:48:55.067 回答
0

这将起作用。

package aa;
    import java.util.Random;
    import java.util.Scanner;


    public class abc {


        public static void main(String[] args) {
            System.out.println(" Welcome ");

            Random rand = new Random();
            Scanner input = new Scanner(System.in);

            boolean play_again = true;
            while (play_again)
            {
                int number_guess = rand.nextInt(100)+1;
                int number_of_tries = 0;
                int guess;
                String another = "y";

                boolean win = false;

                while (win == false)
                {
                    System.out.println(" Try too guess a number between 1 and 100 ");
                    guess = input.nextInt();
                    number_of_tries++;

                    if (guess == number_guess)
                    {
                        win = true;
                        break;
                    }
                    else if (guess < number_guess)
                    {
                        System.out.println(" Guess is too low " + "\n Guess another number to continue or n to quit ");
                        if (input.hasNext("n"))
                        {
                            play_again = false;
                            break;

                        }
                    }
                    else if (guess > number_guess)
                    {
                        System.out.println(" Guess is too high " + "\n Guess another number to continue or n to quit ");
                        if (input.hasNext("n"))
                        {
                            play_again = false;
                            break;
                        }
                    }
                }

                input.next();

                if (win == true){
                    System.out.println(" You Win!!! ");
                }
                else{
                    System.out.println(" Good Luck Next Time!!! ");
                    System.out.println(" The number was " + number_guess);
                }

                System.out.println(" It took you " + number_of_tries + " tries " +
                        "\nWould you like to play again? (y/n): ");

                another = input.next();
                if (another.equalsIgnoreCase("y") == true)
                    play_again = true;
                else 
                {
                    play_again = false;
                }
            }
            System.out.println("Thank you!!!");
        }

    }
于 2013-11-04T06:25:23.333 回答