該程序詢問用戶他希望有多少玩家,一旦提示程序運行,玩家1接收3卷兩個6面骰子(3次)等等下一個球員。玩家可以選擇保留哪些卷軸,也可以保留兩者。Java Dice程序儘管用戶選擇的結果總是去其他語句
然而,問題出現了,每個玩家的擲骰總是相同的,這就像Math.random在我的玩家類程序中沒有效果。此處發生另一個問題:其他問題:JOptionPane.showMessageDialog(Test,「Tie」+ data [0] +「And」+ data [1]); 當選擇2個玩家時,程序總是進入該課程的else語句。如果在2-4名玩家的任何地方,它會在每個玩家數上進入else語句,總是導致進入指定的else語句。
我已經嘗試在兩個Die的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;
}
}
Player類
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;
}
}
如果您想要答案,請發佈較少的代碼。 SO是針對具體的編程問題而不是調試服務。使用調試器,單步執行代碼並找到具體問題。投票結束。 –
我發現代碼難以閱讀。 –
爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –