# Counting How Many Times Program Was Excecuted

• October 29th, 2010, 08:03 PM
Override
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:

Code java:

```  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); } }   }```
• October 29th, 2010, 10:43 PM
aussiemcgr
Re: Counting How Many Times Program Was Excecuted
This is a problem:
Code java:

```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:
Code java:

```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:
Code java:

```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:
Code java:

```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:
Code java:

```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:
Code java:

```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.
• October 30th, 2010, 05:11 PM
Override
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

Code java:

` 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

Code java:

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