有没有一种方法可以提示用户他们想重新滚动并更新变量的原因?

时间:2019-03-26 15:39:05

标签: java if-statement while-loop do-while

我正在创建一个骰子游戏,其中4个骰子通过数字0-6滚动。滚动之后,我想问用户是否要重新滚动其骰子的0、1或2。我在更新d1,d2,d3,d4变量(相应的骰子)时遇到问题,我不确定如何正确构造while循环来实现我的目标。我有尝试将其注释掉的代码,因为它无法正常工作。它会重新向用户询问,并且永远不会逃脱while循环。它还不会更新所选骰子的变量。

非常感谢您的帮助。

我已经尝试过if-else,while,do-while循环和语句。我认为我没有正确构造它们。

public class Program6b {

    public static void main (String[]args)
    {

        Scanner stdIn = new Scanner(System.in);

        // This next section is an introduction to what my program does. \\
        System.out.println("Welcome to Coulter's Dice Game!");
        System.out.println("---------------------------------------------");
        System.out.println("Any Quad and you win! (106 wins)");
        System.out.println("Any Triple and you win! (6 wins)");
        System.out.println("Any Two-Pair is a win! (4 wins)");
        System.out.println("Anything else and you lose. (1 loss)");
        System.out.println("---------------------------------------------");
        System.out.println();
        System.out.println("Type anything to roll your first die.");
        String dontMindMe = "";
        dontMindMe = stdIn.next();
        System.out.println();
        System.out.println("---------------------------------------------");
        System.out.println();

        int d1, d2, d3, d4; // Int variables for 4 die.

        int wins = 0, loses = 0; // Keeps track of wins and loses.

        int rounds = 0; // Keeps track of rounds played.

        String playAgain = ""; // String to store if the user would like to play again.

        boolean win = false; // Boolean to determine if the player one.

        String rerollOne, rerollTwo;

        do
        {
        rounds = rounds + 1; // Adds 1 round every time the program is looped.

        d1 = (int)(Math.random() * 6) + 1; 
        d2 = (int)(Math.random() * 6) + 1; 
        d3 = (int)(Math.random() * 6) + 1; 
        d4 = (int)(Math.random() * 6) + 1;

        System.out.println("  Player ");
        System.out.println("----------");
        System.out.println(d1 + "  " + d2 + "  " + d3 + "  " + d4); // Outputs the dice rolls.
        System.out.println();

        win = false; // Resets the win boolean to false.

        // This next section asks the user if they would like to re-roll their dice
        int choice;

        /* System.out.println("How many dice would you like to reroll? (0,1,2): "); 
        choice = stdIn.nextInt();
        while (choice !=  0)
        {

            if (choice == 1)
            {
                System.out.println("What is the one die you would like to reroll? (d1, d2, d3, d4): ");
                rerollOne = stdIn.next();
                System.out.println();
                if (rerollOne == "d1")
                {
                    d1 = (int)(Math.random() * 6) + 1; 
                }
                else if (rerollOne == "d2")
                {
                    d2 = (int)(Math.random() * 6) + 1; 
                }
                else if (rerollOne == "d3")
                {
                    d3 = (int)(Math.random() * 6) + 1; 
                }
                else if (rerollOne == "d4")
                {
                    d4 = (int)(Math.random() * 6) + 1;
                }

                System.out.println("  Player ");
                System.out.println("----------");
                System.out.println(d1 + "  " + d2 + "  " + d3 + "  " + d4); // Outputs the dice rolls.
                System.out.println();
            }

            else if (choice == 2)
            {
                System.out.println("What is the first die you would like to reroll? (d1, d2, d3, d4): ");
                rerollOne = stdIn.next();
                System.out.println();
                if (rerollOne == "d1")
                {
                    d1 = (int)(Math.random() * 6) + 1; 
                }
                else if (rerollOne == "d2")
                {
                    d2 = (int)(Math.random() * 6) + 1; 
                }
                else if (rerollOne == "d3")
                {
                    d3 = (int)(Math.random() * 6) + 1; 
                }
                else if (rerollOne == "d4")
                {
                    d4 = (int)(Math.random() * 6) + 1;
                }

                System.out.println("What is the second die you would like to reroll? (d1, d2, d3, d4): ");
                rerollTwo = stdIn.next();
                System.out.println();
                if (rerollTwo == "d1")
                {
                    d1 = (int)(Math.random() * 6) + 1; 
                }
                else if (rerollTwo == "d2")
                {
                    d2 = (int)(Math.random() * 6) + 1; 
                }
                else if (rerollTwo == "d3")
                {
                    d3 = (int)(Math.random() * 6) + 1; 
                }
                else if (rerollTwo == "d4")
                {
                    d4 = (int)(Math.random() * 6) + 1;
                }

                System.out.println("  Player ");
                System.out.println("----------");
                System.out.println(d1 + "  " + d2 + "  " + d3 + "  " + d4); // Outputs the dice rolls.
                System.out.println();
            }

            else
            {
                System.out.println("How many dice would you like to reroll? (0,1,2): ");
                choice = stdIn.nextInt();
            }
        } */

        // This next if, else if, and else statement determines what the player wins. \\
        if ( (d1 == d2) && (d2 == d3) && (d3 == d4) ) // Quad win (106 wins)
        {
            win = true;
            wins = wins + 106;
            System.out.println();
            System.out.println("Quad win! (106 wins added)");
        }
        else if ( (d1 == d2) && (d2 == d3) ||
                  (d1 == d2) && (d2 == d4) ||
                  (d1 == d3) && (d3 == d4) ||
                  (d2 == d3) && (d3 == d4) ) // Triple win (6 wins)
        {
            win = true;
            wins = wins + 6;
            System.out.println();
            System.out.println("Triple win! (6 wins added)");
        }
        else if (
                  (d1 == d2) && (d3 == d4) ||
                  (d1 == d3) && (d2 == d4) ||
                  (d1 == d4) && (d2 == d3) ) // Two pair win (4 wins)
        {
            win = true;
            wins = wins + 4;
            System.out.println();
            System.out.println("Two-pair! (4 wins added)");
        }

        if ( win ) // If statement to print out if the user won.
        {
            System.out.println();
            System.out.println("Yay! You won this dice round!");
            System.out.println();
        }
        else
        {
            ++loses; // Else statement to ass to the total loses and print out if the user lost.
            System.out.println();
            System.out.println("Bummer, that's a junker!");
            System.out.println();
        }

        do // Do-while to make sure the user enters 'y' or 'n'.
        {
        System.out.println("Would you like to play again? (y/n): ");
        playAgain = stdIn.next();
        } while (!(playAgain.equalsIgnoreCase("y") || playAgain.equalsIgnoreCase("n")));
        System.out.println(); // Creates an empty space to tidy the output up.
        } while (playAgain.equalsIgnoreCase("y"));

        // This next section displays the final total of wins and loses. \\
        System.out.println("---------------------------------------------");
        System.out.println();
        System.out.println("Here are your results:");
        System.out.println("Total Rounds: " + (rounds));
        System.out.println("Total Rounds Lost: " + loses);
        System.out.println("Total Wins: " + wins);


    }
}

如果要重新滚动1个或2个骰子,我希望用户得到提示,并且该程序可以正常工作。我希望数字是与用户输入内容相对应的更新。完成之后,我希望向用户展示他们的胜利,并询问他们是否愿意再次玩。

1 个答案:

答案 0 :(得分:0)

您没有在条件中正确比较字符串。由于您使用==运算符比较字符串,因此您的代码正在检查引用是否相等。在您的情况下,这将始终为false。

例如,更改此:

if (rerollOne == "d1")

对此:

if ("d1".equals(rerollOne))

equals方法将比较字符串的内容。

此外,由于您仅在else块中提示输入新的choice,所以您将永远无法重新滚动。我建议将针对新choice的提示移到else之外,以便您在循环的每次迭代中提示新的选择,并完全删除else。