1

程序询问用户他希望有多少玩家,一旦提示程序运行,玩家 1 收到 3 次两个 6 面骰子(3 次)的掷骰,以此类推给下一个玩家。玩家可以选择保留哪些滚动或可能保留两者。

然而问题发生了,每个玩家的掷骰总是相同的,就像 Math.random 在我的玩家类程序中没有效果一样。这里出现另一个问题: else { JOptionPane.showMessageDialog(Test,"Tie "+ data[0] + " And " + data[1]); where the program always goes to the else statement of that class when 2 players are selected. 它会针对每个玩家计数进入 else 语句,例如从 2 到 4 名玩家,总是会导致继续执行指定的 else 语句。

我尝试在 PairOFDice 类中围绕两个 Die 运行 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; 
 }
}
4

2 回答 2

1

Java中的Random需要先初始化,否则总是返回相同的数字序列。

Math.random()不是生成随机数的最佳方法,您应该使用Random.nextInt()代替。

于 2013-10-06T21:27:55.003 回答
0

在您的循环中,您说"player " + (i+1),但是您只引用来自 的结果p1。这听起来像您所看到的问题。

于 2013-10-06T21:31:09.573 回答