程序询问用户他希望有多少个玩家,一旦提示程序运行,玩家 1 会收到 3 卷两个 6 面骰子(3 次),依此类推,为下一个玩家提供。玩家可以选择保留哪些骰子或可能同时保留两者。
但是问题出现了,每个玩家的掷骰数总是相同的,就像 Math.random 在我的玩家类程序中没有效果一样。这里出现另一个问题: else { JOptionPane.showMessageDialog(测试,"Tie "+ data[0] + "和 "+ data[1]); 当选择 2 个玩家时,程序总是转到该类别的 else 语句。它会转到每个玩家计数的 else 语句,从 2 到 4 个玩家的任何地方,总是会导致继续执行指定的 else 语句。
我尝试在 PairOFDice 类中围绕两个骰子运行 for 循环,但没有效果,它没有改变骰子的掷骰结果。我还尝试在每次程序完成一个循环后重置 Die 值,但这只会导致它们的值停留在零。任何意见将不胜感激。
import javax.swing.*;//MAIN LOGIC PROGRAM
public class Logic {
public static void main (String [] args) {
PairOfDice p1 = new PairOfDice("Player 1");
PairOfDice p2 = new PairOfDice("Player 2");
Player PClass = new Player();
JFrame IntroPane = new JFrame ();
JFrame Test = new JFrame ();
int Crolls = 0;
int data[] = new int[4] ;
JOptionPane.showMessageDialog(Test,"Hello and welcome to the program! In the Following program, you will be playing a game of dice against another player\nEach of you will roll two six sided dice's three times, choosing to hold the first second or both die's\nThe highest total wins! Good luck!");
String c = JOptionPane.showInputDialog(Test,"To begin, how many players are playing?\n2 Players enter '2'" + "\n3 Players enter '3'" + "\n4 Players enter '4'");
int x = Integer.parseInt(c);
for (int i = 0; i < x; i++) {
for(int s = 0; s < 3; s++) {
p1.play();
p2.play();
JOptionPane.showMessageDialog(IntroPane,"Player " + (i+1));
JOptionPane.showMessageDialog(IntroPane,"Dice 1 rolled : " + p1.getDice1() + "\nDice 2 rolled : " + p1.getDice2());
Object[] options = {"Hold Dice 1",
"Hold Dice 2",
"Hold Both"};
int n = JOptionPane.showOptionDialog(Test,
"Which Roll would you like to keep?\nKeep Dice 1 or Dice 2\nOr keep both!\n\nYour Total so far is :" +data[i]
+ "",
"",
JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[2]);
if(n == JOptionPane.YES_OPTION)
{
PClass.HoldFirstDie(p1.getDice1());
JOptionPane.showMessageDialog(Test,"You choose to hold :" +p1.getDice1() );
data[i] += PClass.getFirstDie();
Crolls++;
}
else if(n == JOptionPane.NO_OPTION) {
PClass.HoldSecondDie(p1.getDice2());
JOptionPane.showMessageDialog(Test,"You choose to hold :" +p1.getDice2() );
data[i] += PClass.getSecondDie();
Crolls++;
}
else if(n== JOptionPane.CANCEL_OPTION) {
PClass.HoldFirstDie(p1.getDice1());
PClass.HoldSecondDie(p1.getDice2());
JOptionPane.showMessageDialog(Test,"You choose to hold :" +p1.getDice1() + " and :" + p1.getDice2() );
data[i] += ( PClass.getFirstDie() + PClass.getSecondDie() ) ;
Crolls++;
}
}
}
if( x == 2) {
if(Crolls == 3 && data[0] > data[1]) {
JOptionPane.showMessageDialog(Test,"Player 1 wins");
}
else if (Crolls == 3 && data[1] > data[0]) {
JOptionPane.showMessageDialog(Test,"Player 2 wins");
}
else {
JOptionPane.showMessageDialog(Test,"Tie "+ data[0] + " And " + data[1]);
}
}
if(x == 3) {
if(Crolls == 3 && data[2] > data[0] && data[2] > data[1]) {
JOptionPane.showMessageDialog(Test,"Player 3 wins");
}
else if(Crolls == 3 && data[0] > data[1] && data[0] > data[2]) {
JOptionPane.showMessageDialog(Test,"Player 1 wins");
}
else if(Crolls == 3 && data[1] > data[0] && data[1] > data[2]) {
JOptionPane.showMessageDialog(Test,"Player 2 wins");
}
else {
JOptionPane.showMessageDialog(Test,"Tie");
}
}
if(x ==4) {
if(Crolls == 3 && data[0] > data[1] && data[0] > data[2] && data[0] > data[3]) {
JOptionPane.showMessageDialog(Test,"Player 1 wins");
}
else if(Crolls == 3 && data[1] > data[0] && data[1] > data[2] && data[1] > data[3]) {
JOptionPane.showMessageDialog(Test,"Player 2 wins");
}
else if(Crolls == 3 && data[2] > data[0] && data[2] > data[1] && data[2] > data[3]) {
JOptionPane.showMessageDialog(Test,"Player 3 wins");
}
else if(Crolls == 3 && data[3] > data[0] && data[3] > data[1] && data[3] > data[2]) {
JOptionPane.showMessageDialog(Test,"Player 4 wins");
}
else {
JOptionPane.showMessageDialog(Test,"Tie");
}
}
}}
PairOFDice 类
public class PairOfDice {
private int Dice1 = 0, Dice2 = 0;
public String name;
PairOfDice(String name){
this.name = name;
}
public void PairOfDice2() {
play();
}
public void play () {
//for(int i = 0; i < 3; i++) {
Dice1 = (int)(Math.random () * 6) + 1;
Dice2 = (int)(Math.random () * 6) + 1;
}
//}
public int getDice1() {
return Dice1;
}
public int getDice2() {
return Dice2;
}
public int getTotal () {
return Dice1 + Dice2;
}
}
玩家等级
public class Player {
private int holdDice1 = 0;
private int holdDice2 = 0;
public void HoldFirstDie (int FirstDie) {
holdDice1 = FirstDie;
}
public void HoldSecondDie(int SecondDie) {
holdDice2 = SecondDie;
}
public int getFirstDie() {
return holdDice1;
}
public int getSecondDie() {
return holdDice2;
}
}
最佳答案
Java中的Random需要先初始化,否则总是返回相同的数字序列。
Math.random()
不是生成随机数的最佳方法,您应该使用 Random.nextInt()相反。
关于Java Dice 程序总是转到 Else 语句,无论用户选择的结果如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19213963/