繁体   English   中英

骰子滚动模拟

[英]Dice Rolling Simulation

我想知道当你的总掷骰是2个特定数字中的一个时,我怎么能让我的骰子滚动模拟器停止(例如:滚动将在你达到7或12的总滚动时停止)。 到目前为止,这是我的代码:

public class RollDice {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int dice1;   // First Dice.
        int dice2;   // Second Dice.
        int total;   // Sum of the two rolls. 

        dice1 = (int)(Math.random()*6) + 1;
        dice2 = (int)(Math.random()*6) + 1;
        total = dice1 + dice2;

        System.out.println("Your first roll is " + dice1);
        System.out.println("Your second roll is  " + dice2);
        System.out.println("Your complete roll is " + total);

    }  

}  

只需使用do-while循环包装所有内容:

int total;   // Sum of the two rolls. 
do {
    int dice1;   // First Dice.
    int dice2;   // Second Dice.

    dice1 = (int)(Math.random()*6) + 1;
    dice2 = (int)(Math.random()*6) + 1;
    total = dice1 + dice2;

    System.out.println("Your first roll is " + dice1);
    System.out.println("Your second roll is  " + dice2);
    System.out.println("Your complete roll is " + total);
} while (total != 7 && total != 12);

或者那种程度的东西。 :)

请参阅https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM