Results 1 to 10 of 10
  1. #1
    Chad.Curtiss is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Question Trouble with While Statement

    I need to make a section of code re- run depending on user input. I tried using a while loop but I could not get it working!

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    public class Game {
        public static void main(String args[]){
            System.out.println("Welcome to the Gumball Guessing Game");
            System.out.println("Please enter your first Guess!");
            Scanner input = new Scanner(System.in);
            Random gumballs = new Random();
            int number;
            int gum;
            
            for(int counter=1; counter<=1;counter++){
                gum = gumballs.nextInt(5);
                number = input.nextInt();
           
            
            if(number == gum){
                System.out.println("CORRECT!!! You won!");
                }
            }
        }
    Last edited by JosAH; 07-15-2012 at 11:31 AM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Trouble with While Statement

    Your for loop only runs its body once (counter > 1 on the second pass); is that what your problem is about?

    kind regards,

    Jos

    ps. fix your indentation.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Trouble with While Statement

    Yeah, the problem here seems to be that there isn't a while statement.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  4. #4
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Trouble with While Statement

    Hi,

    How about trying to make a while statement then?

    Java Code:
    while(number != gum)
    That's the route i'd be going down here.

  5. #5
    Chad.Curtiss is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Trouble with While Statement

    well I tryed to fix it with a while loop again but to no avail. Sorry if I seem really dumb... just new to Java!


    Java code:
    import java.util.Scanner;
    import java.util.Random;
    public class Game {
    public static void main(String args[]){
    System.out.println("Welcome to the Gumball Guessing Game");
    System.out.println("Please enter your first Guess!");
    Scanner input = new Scanner(System.in);
    Random gumballs = new Random();
    int number;
    int gum;
    while(number <||> gum){

    for(int counter=2; counter<=5;counter++){
    gum = gumballs.nextInt(5);
    number = input.nextInt();
    }
    }
    if(number == gum){
    System.out.println("CORRECT!!! You won!");
    }else{
    System.out.println("WRONG");
    }

    }
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Trouble with While Statement

    What is that '<||>' thing?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Chad.Curtiss is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Trouble with While Statement

    greater than or less than.

  8. #8
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Trouble with While Statement

    <||> is not a valid boolean operator in Java, I'm afraid. The most convenient way to do an equivalent test that I know of is:
    Java Code:
     (number < gum || number > gum)
    Or you could just do this, right?
    Java Code:
    (number != gum)
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  9. #9
    Chad.Curtiss is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Trouble with While Statement

    I did it like you said and it now says that the variables gum and number may not have been initialized?

  10. #10
    Chad.Curtiss is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Trouble with While Statement

    Discard my previous reply I have gotten it working... I just had to put the while loop in a slightly different spot. Thanks for all of your help. And for tolerating my Java ignorance. lol

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  2. Trouble with recursion if statement.
    By david522 in forum New To Java
    Replies: 3
    Last Post: 09-28-2011, 03:41 PM
  3. new to java, trouble with if statement
    By Globular in forum New To Java
    Replies: 8
    Last Post: 05-25-2011, 07:03 PM
  4. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-22-2008, 12:52 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 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
  •