Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help Closing While Loop

    So this is what I have so far:
    Code:
    import java.util.Random;
    
    public class lotteryGame
    {
      public static void main(String args[]) throws Exception
     {
     int n=0; // counter for the amount of times played
     int t=0; // total amount of winnings
     int i=0; // number of matches
     int a; // first guess
     int b; // second guess
     int c; // third guess
     int d; // first number in the lottery
     int e; // second number in the lottery
     int f; // third number in the lottery
     Random rand = new Random();
    
    while (n<100) // play the game for 100 times
    {
     a=rand.nextInt(9) + 1; // assign a random number to a
     b=rand.nextInt(9) + 1;
     c=rand.nextInt(9) + 1;
     System.out.println("Lottery Number"+a+b+c);
     d=rand.nextInt(9) + 1;
     e=rand.nextInt(9) + 1;
     f=rand.nextInt(9) + 1;
    System.out.println("Your guess"+d+e+f);
     if (a==d){
      i=i+1;
      System.out.println(a+"is a match");
      d=d+10; // if a=d then d is taken out of the range (1-9)
     }
     else if (a==e){
       System.out.println(a+"is a match");
      i=i+1;
      e=e+10;
     }
     else if  (a==f){
       System.out.println(a+"is a match");
      i=i+1;
      f=f+10;
     }
     if (b==d){ 
       System.out.println(b+"is a match");
      i=i+1;
      d=d+10;
     }
     else if (b==e){
       System.out.println(b+"is a match");
      i=i+1;
      e=e+10;
     }
     else if  (b==f){
       System.out.println(b+"is a match");
      i=i+1;
      f=f+10;
     }
     if (c==d){ 
       System.out.println(c+"is a match");
      i=i+1;
      d=d+10;
     }
     else if (c==e){
       System.out.println(c+"is a match");
      i=i+1;
      e=e+10;
     }
     else if  (c==f){
       System.out.println(c+"is a match");
      i=i+1;
      f=f+10;
     }
     if (i==3){
       if (a==d-10&&b==e-10&&c==f-10){
       t=t+10000;
       System.out.println("You win!!!");
       }
       else{ 
       t=t+1000;
       System.out.println("You had 3 matches! You win $1000");
       }
     }
     if (i==2){
      t=t+50;
      System.out.println("You had 2 matches! You win $50");
     }
     if (i==1){
       
      t=t+10;
      System.out.println("You had 1 match! You win $10");
     }
    }
    
    n=n+1;
    i=0;
     }
    }
    I don't know what is wrong with the program, It compiles fine but when I run it, it never stops. =/
    I figure that
    Code:
    n=n+1
    would be a counter to stop the while loop. Where do I place it so it can count everytime it runs. Or, What do I need to add in order for the program to stop running after 100 times?
    Thank You. =]

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Lilo17x View Post
    I figure that
    Code:
    n=n+1
    would be a counter to stop the while loop. Where do I place it so it can count everytime it runs.
    Inside the body of the loop otherwise it's never reached.
    Also as a matter of general good practice, always perform a comparison between the title of a forum and the language you want to discuss. If the test fails, continue searching.

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Location
    Portland Oregon
    Posts
    690
    Thanks
    44
    Thanked 63 Times in 62 Posts
    that's java code which is not the same as javascript. You probably want to find a java forum to get an answer


  •  

    Posting Permissions

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