Results 1 to 3 of 3
  1. #1
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Can't get java dice program to work

    Hi,

    I am writing a program to roll two dice untill the total amount is 7 or 11. I am curently unsucessful with it and now i need help. Can anyone help?

    Here is the code:

    public class javagamesdice{

    public static void main(String[] args){

    //dice
    long die1 = 1;
    long die2 = 1;
    long totaldice = die1 + die2;

    //print dice
    System.out.println(" ");
    System.out.println("Die #1: " + die1);
    System.out.println("Die #2: " + die2);
    System.out.println("Total dice: " + totaldice);

    //loop
    while ((totaldice != 7) || (totaldice != 11));

    if ((totaldice == 7) || (totaldice == 11)){

    System.out.println ("You Win!");

    }else{

    //new dice
    long newdie1 = Math.round(Math.random()*5)+1;
    long newdie2 = Math.round(Math.random()*5)+1;

    //dice
    die1 = die1 - die1 + newdie1;
    die2 = die2 - die2 + newdie2;
    totaldice = (die1 + die2);

    //print dice
    System.out.println(" ");
    System.out.println("Die #1: " + die1);
    System.out.println("Die #2: " + die2);
    System.out.println("Total dice: " + totaldice);
    }
    }
    }

    (End of code)

    Thanks.

  2. #2
    srossd is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Can't get java dice program to work

    Your main issue is that you ended the while statement with a semicolon, rather than brackets enclosing your if/else statement. Also, the while condition should be and, not or. Finally, once these edits are made, I changed your code a bit so that it prints "You win!" if the user rolls a 7 or 11 - your original loop would just print "Total Dice: 7/11" and then stop. Here's a working version:
    Java Code:
    public class javagamesdice{
    
    public static void main(String[] args){
    
    //dice
    long die1 = 1;
    long die2 = 1;
    long totaldice = die1 + die2;
    
    //print dice
    System.out.println(" ");
    System.out.println("Die #1: " + die1);
    System.out.println("Die #2: " + die2);
    System.out.println("Total dice: " + totaldice);
    
    //loop
    while ((totaldice != 7) && (totaldice != 11)) {
    
    //new dice
    long newdie1 = (int)(Math.random()*5)+1;
    long newdie2 = (int)(Math.random()*5)+1;
    
    //dice
    die1 = newdie1;
    die2 = newdie2;
    totaldice = (die1 + die2);
    
    //print dice
    System.out.println(" ");
    System.out.println("Die #1: " + die1);
    System.out.println("Die #2: " + die2);
    System.out.println("Total dice: " + totaldice);
    if(totaldice == 7 || totaldice == 11)
    System.out.println("You Win!");
    }
    }
    }
    Also, in the future, please tell us more about why it's not working.

  3. #3
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: Can't get java dice program to work

    Thanks for the help, I will try to add more detail in future posts

Similar Threads

  1. Dice Program
    By rahulkumarnaik in forum New To Java
    Replies: 14
    Last Post: 11-15-2011, 08:05 PM
  2. Need help with a dice program. Thanks!!
    By daftwolfie in forum Eclipse
    Replies: 1
    Last Post: 05-15-2011, 11:28 AM
  3. Please help..dice java program
    By aronkevin in forum New To Java
    Replies: 3
    Last Post: 10-08-2010, 06:37 AM
  4. Java Program (rolling the dice)
    By genocist in forum New To Java
    Replies: 8
    Last Post: 03-01-2010, 04:15 PM
  5. Small Dice Program
    By kimmelim in forum New To Java
    Replies: 13
    Last Post: 02-15-2009, 02:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •