1. Member
Join Date
Sep 2009
Posts
16
Rep Power
0

## Help with loops!

I am creating a craps game with java. I am having a little problem identifying with the different loops options that we in the class have to follow using the for loop or the while loop. I have most of the code done. It is just a matter of knowing what each loop will do for me.

Java Code:
```import java.util.Scanner;

public class CrapsGame {
public static void main(String[] args) {

// Generate a two random numbers between 1 - 6
int genNum1 = 1 + (int)(Math.random() * 6);
int genNum2 = 1 + (int)(Math.random() * 6);
int point = 0;
int sum = 7;

System.out.println("The two generated numbers are " + genNum1 + " and " + genNum2);

if (genNum1 + genNum2 == 7 || genNum1 + genNum2 == 11) {
System.out.println("You win!\n");
Scanner input = new Scanner(System.in);
System.out.print("Do you want to play again? Y/N: ");
}

if (genNum1 + genNum2 == 2 || genNum1 + genNum2 == 3 ||
genNum1 + genNum2 == 12) {
System.out.println("!!!!CRAPS!!!! YOU LOSE!");
}

else

if (genNum1 + genNum2 > 0) {
point = (genNum1 + genNum2);
System.out.println(point);
Scanner input = new Scanner(System.in);
System.out.print("POINT! Role Again!! Press 1: ");
int enter = input.nextInt();
}

int genAnotherNum1 = 1 + (int)(Math.random() * 6);
int genAnotherNum2 = 1 + (int)(Math.random() * 6);

if (genAnotherNum1 + genAnotherNum2 == point) {
System.out.println(genAnotherNum1 + genAnotherNum2);
System.out.println("You win!\n");
}

if (genAnotherNum1 + genAnotherNum2 == 7){
System.out.println(genAnotherNum1 + genAnotherNum2);
System.out.println("!!!!CRAPS!!!! YOU LOSE!");
}

else
}
}```
Again, any help will be appreciated!!
Last edited by jrelvi23; 10-12-2009 at 09:46 PM. Reason: Code tags added for readability

2. I added code tags to aid with readability, but your code's indentation is poor making it still very difficult to read which makes it difficult for us to help you. Consider reposting your code as I think it may get you more replies. Much luck.

3. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
How's that? I didn't know to add code tags so thanks for that!

4. Also, regarding which loop to use, do you know which loop should be used here:
You know that you'll need to perform a loop 4 times

vs. here:
You don't know in advance how many times that the loop will loop and it will depend on the user's responses.

?

5. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
I'm not sure what you are asking... but there is no limit as to how many times the user will play. It could be going non-stop.

6. I'm just trying to see if you understand which loop should be used in which situation. So which loop should be used in my first example, for or while? And the same for the second?

7. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
I do know that a while loop and a for lop is checked before the loop body is executed while the do-while loop would be used as a post-test loop.

I would think that the majority of the code will be under a while loop and where it test for point... would be a do while loop. Am I close? I updated my code btw. I think it works better than the previous one.

Java Code:
```import java.util.Scanner;

public class CrapsGame {
public static void main(String[] args) {

// Generate a two random numbers between 1 - 6
int genNum1 = 1 + (int)(Math.random() * 6);
int genNum2 = 1 + (int)(Math.random() * 6);
int point;

Scanner input = new Scanner(System.in);

System.out.println("The two generated numbers are " + genNum1 + " and " + genNum2);

if (genNum1 + genNum2 == 7 || genNum1 + genNum2 == 11) {
System.out.println("You win!\n");
}

if (genNum1 + genNum2 == 2 || genNum1 + genNum2 == 3 ||
genNum1 + genNum2 == 12) {
System.out.println("!!!!CRAPS!!!! YOU LOSE!");
}

if (genNum1 + genNum2 == 4 || genNum1 + genNum2 == 5 ||
genNum1 + genNum2 == 6 || genNum1 + genNum2 == 8 || genNum1 + genNum2 == 9 ||
genNum1 + genNum2 == 10) {
System.out.print("POINT! Role Again!! Press 1: ");
point = input.nextInt();

int genAnotherNum1 = 1 + (int)(Math.random() * 6);
int genAnotherNum2 = 1 + (int)(Math.random() * 6);

if (genAnotherNum1 + genAnotherNum2 == point) {
System.out.println(genAnotherNum1 + genAnotherNum2);
System.out.println("You win!\n");
}

if (genAnotherNum1 + genAnotherNum2 == 7){
System.out.println(genAnotherNum1 + genAnotherNum2);
System.out.println("!!!!CRAPS!!!! YOU LOSE!");
}

else

}
}
}```
Last edited by jrelvi23; 10-14-2009 at 12:21 AM.

8. My understanding about these loops is this:

for loop: when you know in advance how many times the loop will occur. An example includes if you want to loop through an array and print each item in the array, you will know that this will loop exactly array.length times.

while loop: when you don't know in advance how many times the loop should occur and want to check this out at the beginning of the loop. This can loop 0 or more times.

do-while loop: when you don't know in advance how many times the loop should occur, and you want to check this out at the end of the loop. This loop will always occur at least one time if not more.

So given this, should you use a for loop here or one of the while loops? After deciding, why not try to place a loop in your code?

9. Member
Join Date
Sep 2012
Posts
7
Rep Power
0

## Re: Help with loops!

HI ,

for loop: you should use this loop when you know the exact no. of iterations well in advance there u can bring one integer in picture also it is beneficial when you need to use nested loop for example you want to perform one thing say 10 times and for each time you need to perform another thing 5 times so in that case for inside for is the best solution.
While loop: you dont know the values at current time but you know the limit of the value, you shuld go for while loop.
Memory basis thinking I will suggest you to prefer while as there will less stress on Garbage collector and JVM load will also be less.

10. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Help with loops!

This is 3 years old.
Locking.

#### Posting Permissions

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