class - Java Dice Program Always goes to Else statement despite the outcome of the users choice -


the program asks user how many players wishes have, once prompted program runs, player 1 receives 3 rolls of 2 6 sided dices (3 times) , on next players. players can choose rolls keep or potentially keeping both.

the problem occurs however, rolls same every player, it's math.random has no effect in player class program. problem occurs here : else { joptionpane.showmessagedialog(test,"tie "+ data[0] + " , " + data[1]); program goes else statement of class when 2 players selected. goes else statement on every single player count in, anywhere 2-4 players, results in proceeding specified else statement.

i have tried running loop in pairofdice class around 2 die's, no avail did not change rolls of dice. have tried reset die values after every time program has gone through 1 loop, caused values stuck @ zero. input appreciated.

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 , welcome program! in following program, playing game of dice against player\neach of roll 2 6 sided dice's 3 times, choosing hold first second or both die's\nthe highest total wins! luck!"); string c = joptionpane.showinputdialog(test,"to begin, how many players playing?\n2 players enter '2'" + "\n3 players enter '3'" + "\n4 players enter '4'"); int x = integer.parseint(c);  (int = 0; < 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 keep?\nkeep dice 1 or dice 2\nor keep both!\n\nyour total far :" +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 hold :" +p1.getdice1() );        data[i] += pclass.getfirstdie();       crolls++;     }      else if(n == joptionpane.no_option) {       pclass.holdseconddie(p1.getdice2());       joptionpane.showmessagedialog(test,"you choose 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 hold :" +p1.getdice1() + " , :" + 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] + " , " + 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 class

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 = 0; < 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 class

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;   } } 

random in java needs initialized fist, otherwise return same sequence of numbers.

math.random() isn't best way of generating random numbers, should use random.nextint() instead.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -