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.

Results 1 to 4 of 4

Thread: Die Rolling - Continuing the statement?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Die Rolling - Continuing the statement?

    What's up all,

    I have a second problem. This one I've come close to. The point is to make a die-rollng game that the user can choose to terminate, or continue. I've pretty much written it all, except I'm stuck at one point:

    System.out.println("Craps!");
    else {
    System.out.println("Do you want to roll again? (1 = yes, 2 = no): ");
    accept = input.nextInt();

    That part. What I want is for the user to be able to once again, enter 1 or 2 to continue the program or terminate it. However, it doesn't do this. How can I make it so that it does. I assumed that if the user enters one, the first "if" statement will execute again, but it's not. What am I doing wrong?

    Thanks guys. Here is the code:






     
    package dieroll;
    import java.util.Scanner;
    public class Main {
    public static void main(String[] args) {
     
          System.out.println("Welcome to Anthony's Die Roll application!");
     
         int die1 = (int)(Math.random() * 6);
         int die2 = (int)(Math.random() * 6);
         int i;
     
         if (die1 == 0 || die2 == 0)
             die1 = (die1 + 1);
             die2 = (die2 + 1);
     
         Scanner input = new Scanner(System.in);
         System.out.print("Do you want to roll the die? (1 = yes, 2 = no): ");
         int accept = input.nextInt();
     
     if (accept == 1){
             System.out.println(die1 + " " + die2);
           if (die1 == 1 && die2 == 1)
              System.out.println("Snake eyes!");
            else if (die1 == 6 && die2 == 6)
              System.out.println("Box cars!");
            else if (die1 == 2 && die2 == 5)
              System.out.println("Craps!");
           else {
              System.out.println("Do you want to roll again? (1 = yes, 2 = no): ");
              accept = input.nextInt();
     
                  }
                         }
     else {
         System.out.println("Thanks for playing!");
         System.exit(0);
           }
     
    }
     
    }
    Last edited by Override; October 29th, 2010 at 12:33 AM.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Die Rolling - Continuing the statement?

    Are my loops for the different results completely wrong?

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Die Rolling - Continuing the statement?

    Okay, I fixed it. My only problem is that the number on the die don't change each time it is re-executed. The "Roll" changes, but that's about it. Any suggestions?

     
    package dieroll;
    import java.util.Scanner;
    public class Main {
    public static void main(String[] args) {
     
          System.out.println("Welcome to Anthony's Die Roll application!");
     
         int i;
         int number_of_plays = 1;
     
     
         Scanner input = new Scanner(System.in);
         System.out.print("Do you want to roll the die? (1 = yes, 2 = no): ");
         int accept = input.nextInt();
     
     
     while (accept == 1) {
     
          int die1 = (int)(Math.random() * 6);
          int die2 = (int)(Math.random() * 6);
     
             if (die1 == 0 || die2 == 0)
             die1 = (die1 + 1);
             die2 = (die2 + 1);
     
          for (i = 1; i < 1000; i++){
          number_of_plays = i;
     
         System.out.println("Roll " + number_of_plays + ":" + " " + die1 + " " + die2);
     
              if (die1 == 1 && die2 == 1){
               System.out.println("Snake eyes!");
              if (die1 == 6 && die2 == 6);
               System.out.println("Box cars!");
              if (die1 == 2 && die2 == 5)
               System.out.println("Craps!"); }
     
              else
             System.out.println("Do you want to roll again? (1 = yes, 2 = no): ");
             accept = input.nextInt();
                         }
       while (accept != 1) {
         System.out.println("Thanks for playing!");
         System.exit(0);
           }
        }
        }
     
    }

Similar Threads

  1. If Statement
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 12:57 PM
  2. If Statement in SQL String
    By Steffi1013 in forum Loops & Control Statements
    Replies: 0
    Last Post: March 30th, 2010, 03:25 PM
  3. If-Else Statement help
    By SnowCrashed in forum Loops & Control Statements
    Replies: 5
    Last Post: February 9th, 2010, 07:57 PM
  4. Having trouble with an if statement. please help!!
    By humdinger in forum Loops & Control Statements
    Replies: 11
    Last Post: January 21st, 2010, 02:49 PM
  5. If statement
    By Dave in forum Loops & Control Statements
    Replies: 2
    Last Post: August 27th, 2009, 10:11 AM