Results 1 to 10 of 10
  1. #1
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default 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 
          System.out.print("Please Roll Again! ");
       }
     }
    Again, any help will be appreciated!!
    Last edited by jrelvi23; 10-12-2009 at 08:46 PM. Reason: Code tags added for readability

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    How's that? I didn't know to add code tags so thanks for that!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #5
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    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. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #7
    jrelvi23 is offline Member
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    0

    Default

    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 
            System.out.print("Please Roll Again! ");
    
        } 	  
      }	 
    }
    Last edited by jrelvi23; 10-13-2009 at 11:21 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #9
    hardcoder is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help with loops!

    HI ,

    Please find my comments about the loops:
    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. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: Help with loops!

    This is 3 years old.
    Locking.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 06:59 PM
  2. how to use do while loops
    By mikeitalydz in forum New To Java
    Replies: 32
    Last Post: 09-26-2009, 08:30 PM
  3. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 08:47 AM
  4. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 03:59 AM
  5. Help me: loops in java
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 06:47 PM

Posting Permissions

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