有人可以帮我解决这个java代码吗?循环错误

时间:2014-11-26 18:08:07

标签: java

我对编程很新,我想尝试更高或更低的游戏,但我遇到了一些问题。首先,我希望程序在卡被正确猜测时循环,即选择较低的选项并且它较低。然而,它在第二次循环后崩溃,没有任何反应。当比赛结束时,我会选择再次比赛,没有任何反应。如果有人能帮助我解决这些问题,我将不胜感激。感谢

import

1 个答案:

答案 0 :(得分:1)

试试这个:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int card = 0;
    int cardNext;

    String userResponse;
    String userPlayAgain;
    boolean userGuessedRight = true;

    do {
        do {
            Random randomNumbers = new Random();
            card = randomNumbers.nextInt(13 - 1 + 1) + 1;
            System.out.println("Card is " + card);

            System.out.print("Higher or Lower?");
            cardNext = randomNumbers.nextInt(13) + 1;
            userResponse = scan.next();
            userGuessedRight = (userResponse.equalsIgnoreCase("H") && cardNext >= card)
                    || (userResponse.equalsIgnoreCase("L") && cardNext <= card);

            if (userGuessedRight) {
                System.out.println("Card is " + cardNext);
                System.out.print("Higher or Lower?");
                userResponse = scan.next();
            } else {
                break;
            }
        } while (userResponse.equalsIgnoreCase("H") && cardNext < card || userResponse.equalsIgnoreCase("L") && cardNext > card);
        System.out.println("Card is " + cardNext);
        System.out.println("Card is higher/lower you lose – play again? Y/N : ");
        userPlayAgain = scan.next();
    } while (userPlayAgain.equalsIgnoreCase("Y"));

    do {
        if (userPlayAgain.equalsIgnoreCase("N"))

        {
            System.out.print("Thank You.");
            break;
        }

    } while (!userPlayAgain.equalsIgnoreCase("Y"));

    scan.close();
}

修改

这是输出: The output of the program