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 3 of 3

Thread: Counting How Many Times Program Was Excecuted

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

    Default Counting How Many Times Program Was Excecuted

    Hey, I wanted to post this in a more specific topic area. What I'm creating in here is a program that will roll the dice upon the user's agreement, and ask them if they want to roll again. Everything works fine, but there's just one last problem. I need to add a counter in order to show how many times they played. (It would say "Roll 1: 3 4, Roll 2: 6 6, etc."

    I thought using a for loop like this would work:

    for (i = 1; i < number_of_plays; i++) would work, but I seemed to have put that in all kinds of different places in my code, and not one worked. The thing is, I had it working before, but when I started to change my loops around because my die weren't recalculating every time the user initated another play, it stopped advancing. Any suggestions?

    Thanks.


    Here's the code:

     
    package dicegamefinal;
     
    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 = 1;
         int number_of_plays = i;
     
     
     
     
         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) {
     
     
     
         if (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);
     
     
     
         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.print("Do you want to roll again? (1 = yes, 2 = no): ");
             accept = input.nextInt();
     
             }
     
                           }
             else
                System.exit(0);
     
     
                         }
     
       while (accept != 1) {
         System.out.println("Thanks for playing!");
         System.exit(0);
                           }
                                             }
     
                       }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,660
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: Counting How Many Times Program Was Excecuted

    This is a problem:
    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!"); }
    Those brackets are making this block impossible. It will only enter this block if die1 and die2 are equal to 1, however if it does, then die1 and die2 will not equal 6 and will not equal 2 and 5. That is what you are saying. Instead, you want:
    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!");

    Next, you have a problem with how you execute your loop. Since you are checking the status of accept, you want to change that value every time or you will have an infinite loop. So, instead of all of this:
    while (accept == 1) {
    ...
     
     
              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.print("Do you want to roll again? (1 = yes, 2 = no): ");
             accept = input.nextInt();
     
             }
     
                           }
             else
                System.exit(0);
     
     
                         }
     
       while (accept != 1) {
         System.out.println("Thanks for playing!");
         System.exit(0);
                           }
    You simply want to continue the loop and prompt each time. So, you want:
    while (accept == 1) 
    {
    	...
     
    	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!"); 
     
    	System.out.print("Do you want to roll again? (1 = yes, 2 = no): ");
    	accept = input.nextInt();
    }
    System.out.println("Thanks for playing!");
    System.exit(0);

    Your last issue is this:
    if (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);
    First of all, you never closed the bracket for the first if statement. Second, the second line of the second if statement is not part of the second if statement since you have not blocked it. Also, you need to declare die1 and die2 before the if statements, or scope will make it so you cannot use them. Lastly, the first if statement is not even necessary, as you are guaranteed accept equals 1, or the loop would not be running. What you want instead is:
    int die1 = (int)(Math.random() * 6);
    int die2 = (int)(Math.random() * 6);
     
    if (die1 == 0 || die2 == 0)
    {
    	die1 = (die1 + 1);
    	die2 = (die2 + 1);
    }

    Your problem is your use of brackets. Check them because those are the causes of all of your issues.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following 2 Users Say Thank You to aussiemcgr For This Useful Post:

    JavaPF (October 30th, 2010), Override (October 30th, 2010)

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

    Default Re: Counting How Many Times Program Was Excecuted

    That was very helpful. I took what you said into consideration, and made a lot of changes. With the way you explained it, telling me it's wrong, why its wrong, and how to fix it, I also put a lot of new information into my stash for future programs. So this is what I have now. No errors (I hope!), and everything works. My last question is, I want to stick a "for" loop in there to count how many rolls the user has done. Right now, it's stuck on one.

    I was thinking of using

     for (int number_of_plays; number_of_plays < 1000; number_of_plays++)

    I stuck that right after the roll results, and before the prompt as to whether or not they want to continue, but it jumps from 1 to 1000 in one shot.

    Any suggestions for this one?

    Thanks again.

    Override


     
    package dicegamefinal;
    import java.util.Scanner;
    public class Main {
     
        public static void main(String[] args) {
     
          System.out.println("Welcome to Anthony's Die Roll application!");
     
         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);
     
         System.out.println("Roll " + number_of_plays + ":" + " " + 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 if (die1 == 2 && die2 == 2)
               System.out.println("Double Dose!");
     
     
             System.out.print("Do you want to roll again? (1 = yes, 2 = no): ");
             accept = input.nextInt();
            }
     
        System.out.println(" ");
        System.out.println("Thanks for playing!");
         System.exit(0);
        }
    }

Similar Threads

  1. Counting cells
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 05:04 PM
  2. Replies: 1
    Last Post: October 16th, 2010, 03:32 PM
  3. Question: counting
    By miss confused in forum Java Theory & Questions
    Replies: 2
    Last Post: July 30th, 2010, 05:38 PM
  4. Program help with counting even and odd integers in input value.
    By fueledbyrick in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 6th, 2010, 07:02 PM
  5. GuessWhat- same errors repeated 4 times?
    By iank in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2009, 07:32 PM