尝试随机掷两个骰子并将总和加起来直到达到二十一
Attempting to roll two dice randomly and add the sums till it reaches twentyone
我正在尝试编写一个程序,随机掷两个骰子,将它们加在一起,并一直这样做,直到达到 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;
}
}
正如@KamalNayan 上面所述,您需要循环 rollingDice 直到达到或超过 21,并且不需要将 int agruments 传递给 rollingDice 方法,因为滚动的骰子值是在该范围内生成的方法。一些正在发生的事情的打印也有助于演示运行时发生了什么:
public static void main(String[] args) {
welcome();
int total = 0;
while (total < 21) {
total += rollingDice();
};
System.out.println("Total for all rolls was: " + total);
if (total == 21) {
System.out.println("You win!");
}
else {
System.out.println("You lose.");
}
}
public static void welcome() {
System.out.println("Welcome to the game of Twenty-One! FEELING LUCKY?! goodluck!");
}
public static int rollingDice() {
int dice1 = (int) (Math.random() * 6 + 1);
int dice2 = (int) (Math.random() * 6 + 1);
int sum = dice1 + dice2;
System.out.println(String.format("dice1: %d dice2: %d for a total: %d", dice1, dice2, sum ));
return sum;
}
这里是获胜游戏的输出:
Welcome to the game of Twenty-One! FEELING LUCKY?! goodluck!
dice1: 4 dice2: 1 for a total: 5
dice1: 1 dice2: 4 for a total: 5
dice1: 1 dice2: 3 for a total: 4
dice1: 6 dice2: 1 for a total: 7
Total for all rolls was: 21
You win!
进程已完成,退出代码为 0
我正在尝试编写一个程序,随机掷两个骰子,将它们加在一起,并一直这样做,直到达到 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;
}
}
正如@KamalNayan 上面所述,您需要循环 rollingDice 直到达到或超过 21,并且不需要将 int agruments 传递给 rollingDice 方法,因为滚动的骰子值是在该范围内生成的方法。一些正在发生的事情的打印也有助于演示运行时发生了什么:
public static void main(String[] args) {
welcome();
int total = 0;
while (total < 21) {
total += rollingDice();
};
System.out.println("Total for all rolls was: " + total);
if (total == 21) {
System.out.println("You win!");
}
else {
System.out.println("You lose.");
}
}
public static void welcome() {
System.out.println("Welcome to the game of Twenty-One! FEELING LUCKY?! goodluck!");
}
public static int rollingDice() {
int dice1 = (int) (Math.random() * 6 + 1);
int dice2 = (int) (Math.random() * 6 + 1);
int sum = dice1 + dice2;
System.out.println(String.format("dice1: %d dice2: %d for a total: %d", dice1, dice2, sum ));
return sum;
}
这里是获胜游戏的输出:
Welcome to the game of Twenty-One! FEELING LUCKY?! goodluck!
dice1: 4 dice2: 1 for a total: 5
dice1: 1 dice2: 4 for a total: 5
dice1: 1 dice2: 3 for a total: 4
dice1: 6 dice2: 1 for a total: 7
Total for all rolls was: 21
You win!
进程已完成,退出代码为 0