Java:掷骰子游戏:退出循环然后重新进入

时间:2018-10-09 19:51:53

标签: java

这是一个模拟骰子游戏Craps的程序。规则是如果您掷出7或11,您将获胜。如果掷出2、3或12,则输。如果您滚动其他任何东西,您将获得一个“分”,并被允许再次滚动。如果您分配2分,您也将获胜。

我在这里遇到的麻烦是积分系统。我不知道如何存储该点,然后退出循环并提示用户再次滚动以重新进入循环。

import javax.swing.JOptionPane;

public class DiceGame {

    public static void main(String args[ ]) {

        // declarations

        int dieNumber1,
                dieNumber2,
                point,
                score;

        // initializations

        dieNumber1 = (int)(Math.random( ) * 6 + 1);
        dieNumber2 = (int)(Math.random( ) * 6 + 1);
        score = dieNumber1 + dieNumber2;  

        // executable statements

        for (int i = 0; i < 10000; i++)
        {

            System.out.println ("Roll the die");
            System.out.println ("You rolled a " + score);

            if (score == 11 || score == 7)
            {
                System.out.println ("You Win!");
                break;
            }

            if (score == 2 || score == 3 || score == 12)
            {
                System.out.println ("You Lose.");
                break;   
            }

            if (score == 4 || score == 5 || score == 6 || score == 8 || score == 9 || score == 10)
            {
                point = 1;
                System.out.println ("You got a point.");
            }


        }


        System.exit(0);

    } // end of the main method

} // end of the class 

2 个答案:

答案 0 :(得分:0)

您可能需要明确考虑程序流程应如何运行。即:每个掷骰有三个结果:胜利,失败和得分。我们什么时候想循环?我想(您的答案可能有所不同)当我们想再次滚动时。我们什么时候要再次滚动?我假设(您的答案可能有所不同)当用户获得要点时。因此,关键案例应该是我们循环的结果,而其他任何结果都应该导致循环。

请注意,因为在进行任何其他逻辑操作之前我们总是需要滚动(如果我们不滚动,则无法决定赢/输/点),因此我们将滚动逻辑置于循环内:

那么我们的代码是什么样的?

import javax.swing.JOptionPane;

public class DiceGame {

    public static void main(String args[ ]) {

        // Initialize some things (omitted)

        while(true) { // NOTE: it's generally good form to use while(true) for a loop that could continue forever, instead of a for-loop with a very high number
            // Roll the dice (omitted)
            if (score == 11 || score == 7)
            {
                System.out.println ("You Win!");
                break; // Game over. Get out of our infinite loop.
            }
            if (score == 2 || score == 3 || score == 12)
            {
                System.out.println ("You Lose.");
                break; // As above
            }
            else // If our score is anything else we must have gotten a point
            {
            point = 1; // Don't break. We want to roll again
            }
        }
    }
}

答案 1 :(得分:0)

您需要将初始化移动到循环内,等待某些用户输入再次滚动,并在获得积分时增加积分。

这应该给您您所需要的

import javax.swing.JOptionPane;
import java.util.Scanner;

public class DiceGame {

    public static void main(String args[ ]) {

        // declarations
        int dieNumber1 = 0,
            dieNumber2 = 0,
            point = 0,
            score = 0;

        Scanner scanner = new Scanner(System.in);        

        // executable statements
        // Repeat until score is 2 (or break is called)
        while( point < 2 ){

            // initializations
            dieNumber1 = (int)(Math.random( ) * 6 + 1);
            dieNumber2 = (int)(Math.random( ) * 6 + 1);
            score = dieNumber1 + dieNumber2;  


            System.out.println ("Roll the die");
            System.out.println ("You rolled a " + score);

            if( score == 11 || score == 7 ){
                System.out.println ("You Win!");
                break;
            }

            if( score == 2 || score == 3 || score == 12 ){
                System.out.println ("You Lose.");
                break;   
            }

            if( score == 4 || score == 5 || score == 6 || score == 8 || score == 9 || score == 10 ){
                point = point + 1; // You could use point++ or point += 1 as well

                System.out.println ("You got a point. Total number of points: " + point);

                if( point == 2 ){
                    System.out.println("You Win!");
                }
                else{                
                    // Wait until user presses Enter, then roll again
                    System.out.println ("Press Enter to roll again.");
                    scanner.nextLine();
                    scanner.reset();
                }
            }


        }


        System.exit(0);

    } // end of the main method

} // end of the class