打印骰子游戏的结果

时间:2016-10-29 18:51:59

标签: java eclipse

我必须为一堂课做一个骰子游戏。

我必须打印出最终用户获得的胜利和失败。

到目前为止,这是我的代码,它适用于游戏本身。

我只是不知道如何结束。

此外,它会为每一轮打印相同数字的打印,我不知道如何修复它,因此每轮打印随机数。

import java.util.Scanner;
public class Program06
{
    public static void main(String[] args)
    {
        Scanner stdln = new Scanner(System.in);
        int d1 = (int)(Math.random() * 12) + 1;
        int d2 = (int)(Math.random() * 12) + 1;
        int d3 = (int)(Math.random() * 12) + 1;
        int o1 = (int)(Math.min(d1, Math.min(d2, d3)));
        int o2 = 0;
        int o3 = (int)(Math.max(d1, Math.max(d2, d3)));
        int y = 0;

        if (o1 != d1 && o3 != d1)
        {
            o2 = d1;
        }

        System.out.println("Roll your dice.");
        System.out.println("Roll a triple and you win.");
        System.out.println("Roll a straight and you win.");
        System.out.println("Roll any pair of 7s or higher and you win.");
        System.out.println("Roll any pair of 6s or lower and you lose.");
        System.out.println("Roll anything else and you lose.");

        do
        {
            System.out.print(+d1);
            System.out.println();
            System.out.print(+d2);
            System.out.println();
            System.out.println(+d3);


            if (d1 == d2 && d2 == d3)
            {
                System.out.println("You won!");
            }
            if (d1 >= 7 && d2 >= 7 || d1 >= 7 && d3 >= 7 || d2 >= 7 && d3 >= 7)
            {
                if (d1 == d2 || d2 == d3 || d1 == d3)
                {
                    System.out.println("You won!");
                }
                else
                {
                    System.out.println("You lose.");
                }
            if (o1 < o2 && o2 < o3 && o3 > o1)
            {
                System.out.print("You won!");
            }
            if (o2 < o1 && o3 < o2 && o1 > o3)
            {
                System.out.print("You won!");
            }
            if (o1 > o2 && o2 > o3 && o3 < o1)
            {
                System.out.print("You won!");
            }
            }
            else
            {
                System.out.println("You lose.");
            }
            System.out.print("Would you like to play again? Enter 1 for yes, 2 for no: ");
            y = stdln.nextInt();


        } while (y == 1);
        if (y == 2)
        {
            System.out.print("You played );
        }
        stdln.close();
    }
}

1 个答案:

答案 0 :(得分:0)

在循环之前,声明2个变量int winCount = 0; int loseCount = 0;

每次打印“你赢了!”你应该添加一个语句winCount++(这会在每次有人获胜时递增计数器)。同样,在你打印“你输了”的地方。你应该添加loseCount++

您的最终打印声明应为System.out.print("You played " + winCount+loseCount + " games! You won " + winCount + " of them and lost " + loseCount + " of them.");