Java骰子程序总是转到Else语句,不管用户选择的结果如何
程序询问用户希望有多少玩家,一旦程序运行,玩家1将收到3卷6面骰子(3次),以此类推,等待下一个玩家。玩家可以选择保留哪一个掷骰,或者可能同时保留这两个掷骰 然而问题出现了,每个玩家的掷骰总是一样的,就像数学一样。随机在我的玩家课程中没有效果。这里出现了另一个问题:else{ showMessageDialog(测试,“Tie”+数据[0]+“和”+数据[1]); 其中,当选择2名玩家时,程序始终转到该类的else语句。它转到每个玩家计数的else语句,如在2-4名玩家之间的任意位置,始终导致转到指定的else语句 我曾尝试在PairOFDice类中围绕两个骰子运行for循环,但没有效果,它没有改变骰子的滚动。我还尝试在程序每次经过一个循环后重置骰子值,但这只会导致它们的值停留在零。任何输入都将非常感谢Java骰子程序总是转到Else语句,不管用户选择的结果如何,java,class,constructor,dice,Java,Class,Constructor,Dice,程序询问用户希望有多少玩家,一旦程序运行,玩家1将收到3卷6面骰子(3次),以此类推,等待下一个玩家。玩家可以选择保留哪一个掷骰,或者可能同时保留这两个掷骰 然而问题出现了,每个玩家的掷骰总是一样的,就像数学一样。随机在我的玩家课程中没有效果。这里出现了另一个问题:else{ showMessageDialog(测试,“Tie”+数据[0]+“和”+数据[1]); 其中,当选择2名玩家时,程序始终转到该类的else语句。它转到每个玩家计数的else语句,如在2-4名玩家之间的任意位置,始终导致转
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");
}
}
}}
Java中的Random需要首先初始化,否则它将始终返回相同的数字序列
Math.random()
不是生成随机数的最佳方法,应该改用。Java中的随机数需要先初始化,否则它将始终返回相同的数字序列
Math.random()
不是生成随机数的最佳方法,你应该改用。在你的循环中,你说“player”+(i+1)
,但是你只引用了p1
的结果。这听起来像是你看到的问题。在你的循环中,你说“player”+(i+1)
,但是您只参考了p1
的结果。这听起来像是您看到的问题。如果您想要答案,请少发布代码。询问特定的编程问题而不是调试服务也是如此。使用调试器,逐步检查代码并找到特定的问题。投票关闭。我发现代码不同Illt to read。为了更快地获得更好的帮助,请发布一条。如果您想得到答案,请发布更少的代码。询问特定的编程问题而不是调试服务也是如此。使用调试器,逐步检查代码并找到特定的问题。投票关闭。我发现代码很难阅读。为了更快地获得更好的帮助,请发布一条。我已设法修复了随机错误数字问题,因为p1.play行应该放在嵌套for循环的内部。我已设法解决随机数字问题,因为p1.play行应该放在嵌套for循环的内部
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;
}
}