Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Can someone help me with this error?

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone help me with this error?

    The while loop keeps running although I type quit. What is wrong with this code?

    import java.util.*;
     
    public class OddEvenGame {
       public static void main(String[] args) {
          Scanner console = new Scanner(System.in);
          Random r = new Random();
     
          int chips = 30;
          int betting = 0;
          String call = "";
          String answer = "";
     
          while (chips != 0 || !answer.equals("quit")) {
             int dice = r.nextInt(6) + 1;
             System.out.print("Odd or even? (type \"quit\" to exit) ");
             call = console.next();
             System.out.print("How many chips are you betting? ");
             betting = console.nextInt();
     
             if (dice % 2 == 0 && call.equals("even")) {
                chips = betting * 2 + chips;
                System.out.println("You have earned " + betting * 2 + " chips!"); 
             } else if (dice % 2 == 1 && call.equals("odd")) {
                chips = betting * 2 + chips;
                System.out.println("You have earned " + betting * 2 + " chips!"); 
             } else {
                chips = chips - betting;
                System.out.println("You lost " + betting + " chips!");
             }
     
            System.out.println("You have " + chips + " chip or chips now.");
            System.out.println();
          }
       }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Can someone help me with this error?

    Rethink your while condition. It's better to use '>' and '<' when possible, so change != to >, but then think hard about what the while condition is saying.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone help me with this error?

    I changed != to >, but when I type "quit" to exit, it still runs. shouldn't it stop running because I said !answer.equals("quit")?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can someone help me with this error?

    Using OR with two conditions means the full expression is true if either sub condition is true.
    Using AND with two conditions means the full expression is true if both sub conditions are true.

    Look at the conditions used in the while()? When will it be false?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Can someone help me with this error?

    I think you never used the variable answer
    while (chips != 0 || !answer.equals("quit"))

    as what I read in your code, you never write a string in that variable.
    and it seems that you code is relying in answer variable to be terminated.

    Suggestion:
    pls replace your while loop condition with this:
    while (chips != 0)

    the quit word will be the user's input right?
    and you store it in call variable of type string.
    therefore the one that your program relied to to terminate is the call variable

    pls insert this line of code after call = console.next(); that was the third line in your loop. please insert this
    if(call.equalsIgnoreCase("quit")) {
          System.exit(0);
    }

    System.exit(0); will make your code terminated. and it will check first if the user's input is "quit" (case insensitive since we used equalsIgnoreCase)
    when the user's input is "quit" it will terminate your program.
    here is the output in mine:

    Odd or even? (type "quit" to exit) quit
    BUILD SUCCESSFUL (total time: 1 second)

Similar Threads

  1. CheckBox/Spinner error Error
    By vandy22 in forum Android Development
    Replies: 1
    Last Post: February 15th, 2014, 04:05 AM
  2. Replies: 3
    Last Post: November 30th, 2013, 05:52 PM
  3. Problems with A* Map Search - GC Overload Error and Null Error
    By puneeth.meruva in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2013, 03:01 PM
  4. Replies: 4
    Last Post: October 15th, 2013, 04:33 AM
  5. Please! Help me to this error "ERROR CANNOT FIND SYMBOL"
    By mharck in forum Object Oriented Programming
    Replies: 8
    Last Post: July 3rd, 2012, 09:20 AM