Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 29 of 29

Thread: Help altering dice program?

  1. #26
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    Yes that makes sense. I thought the following would fix it, but it just results in another error.

    public void multiToss(){
        int score;
        while(minValue(int value)==true)// Change
          score=throwDice();
          scoreboard[score]++;
        }
     
      public boolean minValue(int value){
        for(int j=2;j<13;j++){
          if(scoreboard[j] < value)
            return true;
     
        }


    --- Update ---

     public void multiToss(int value){ //contiues to roll dice and keep score while minValue is true
        int score=0;
        while(minValue(value)==true)
          score=throwDice();
          scoreboard[score]++;
        }
     
      public boolean minValue(int value){ //checks the scores. reports true when scoreboard values are less than the entered value, false when the scores are greater than or equal to the entered value
        boolean x=false;
        for(int j=2;j<13;j++){
          if(scoreboard[j] < value)
            x=true;}
          return x;
     
        }

    I made some changes and commented the code.
    This code produces no output and seems to just run forever

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help altering dice program?

    Please post the code for the whole program.

    Don't hide }s at the end of a statement. That makes the code hard to read and understand. }s should be on a line by itself.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Junior Member
    Join Date
    Oct 2013
    Location
    Caldwell Idaho
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help altering dice program?

    Driver:
    import javax.swing.JOptionPane;
    public class DiceExperiment{
      public static void main(String[] args){
        String casino=
          JOptionPane.showInputDialog("enter a min value");
        int value=Integer.parseInt(casino);
        Dice d=new Dice();
        d.multiToss(value);
        d.minValue(value);
        d.displayScoreboard();
      }
    }


    Class:
    public class Dice{
      private int[] scoreboard=new int[13];
      public Dice(){
        initializeScoreboard();}
     
      public void initializeScoreboard(){
        for(int j=0;j<13;j++)scoreboard[j]=0;
      }
     
      public int tossDie(){
        return (1+ (int)(6*Math.random()));
      }
     
      public int throwDice(){
        return(tossDie()+tossDie());
      }
     
      public void multiToss(int value){ //contiues to roll dice and keep score while minValue is true
        int score=0;
        while(minValue(value)==true)
          score=throwDice();
          scoreboard[score]++;
        }
     
      public boolean minValue(int value){ //checks the scores. reports true when scoreboard values are less than the entered value, false when the scores are greater than or equal to the entered value
        boolean x=false;
        for(int j=2;j<13;j++){
          if(scoreboard[j] < value)
            x=true;
    }
          return x;
     
        }
     
     
     
      public int[] getScoreboard(){return scoreboard;}
     
      public void displayScoreboard(){
        for(int j=2;j<13;j++)
          System.out.println(+ j + " " + scoreboard[j]);
      }
     
    }

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help altering dice program?

    seems to just run forever
    The code is probably in an infinite loop. To find where the loop is, you need to do some debugging. Add some println statements that print out messages as methods are called and as loops are executed. Print out the values of the variables that are used to control the logic so you can see where the program is going wrong. The print out will show you where the looping is happening. When you find that, then you will want to find out why the code is looping. Come back if you need help after you've found where the looping is happening and what the values of the variables are as the loop goes around.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Roll Dice Program Button
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2013, 02:51 PM
  2. Dice score program
    By ksahakian21 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 23rd, 2012, 05:25 PM
  3. Help with creating a dice rolling program in Java
    By lilmiss in forum Object Oriented Programming
    Replies: 4
    Last Post: October 26th, 2011, 09:27 PM
  4. Java Dice Program
    By ebone in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 22nd, 2011, 11:07 PM
  5. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 07:50 AM