【发布时间】:2018-09-28 03:34:27
【问题描述】:
我正在尝试编写一个程序,该程序随机掷两个骰子,将它们加在一起,并一直这样做,直到达到 21。如果达到 21,则获胜,但如果超过 21,则失败。
这是我到目前为止所拥有的,如果我能在如何正确掷骰子方面获得一些帮助,那就太好了。我是 java 初学者,所以仍在尝试理解语法。
import java.util.Random;
public class TwentyOne{
public static void main(String[] args) {
int dice1;
int dice2;
welcome();
rollingDice(int dice1,int dice2);
}
public static void welcome() {
System.out.println("Welcome to the game of Twenty-One! FEELING LUCKY?! goodluck!");
}
public static int rollingDice(int dice1, int dice2) {
dice1 = (int)(Math.random()*6 + 1);
dice2 = (int)(Math.random()*6 + 1);
int sum = dice1 + dice2;
return sum;
}
}
【问题讨论】:
-
在 while 循环中调用
rollingDice方法,根据您的要求使用终止条件。此外,不需要传递参数,因为您是在方法本身中生成的 -
@KamalNayan 这也是我更改的内容,我收到错误提示我需要在方法中的每个参数后添加分号
标签: java loops class methods main