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 3 of 3

Thread: While Loop quick question

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default While Loop quick question

    Hi,

     int user = sr.nextInt();
          while(300<=user){
              System.out.println("confused");
              user = sr.nextInt();
     
          }

    Alright, my homework is almost done, but i am having a brain fart. I suppose it happens to the best of us. What I am trying to accomplish is allowing the individual or customer to be able to bet his entire bank account on a single roll of the dice. If he tries to be slick and bet more than he has in his account, the program will repeat him until he bets under or exactly his bank account. I was almost there, I decided to use a while loop and create the condition using less than or equal to as my course of action. I thought "<=" that would enable the user to bet at 3000 or under it.It works under 299 but not at 300, so the customer can only 99% percent of his account. I need the customer to be able to go bankrupt, and this won't happen because once he losses the money and it hits a 1.00 i would have to use a float or double and that is beyond the scope of the assignment. I truly believe that the "<=" should accomplish my goal, but it doesn't. So please educate me on why this is not working.

    Thanks in advance.


  2. #2
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: While Loop quick question

    What I have thought was this condition right, and it works now the gambler can bet his or her entire bank account and go broke in style, but this of course doesn't answer my original question shouldn't "<=" work?

    int user = sr.nextInt();
          while(3<=user&&3!=user){
              System.out.println("confused");
              user = sr.nextInt();
     
          }
     
          }

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: While Loop quick question

    Quote Originally Posted by loui345 View Post
          // The loop continues to execute as long as user is greater than or equal to 300
          while(300<=user) {
              System.out.println("confused");
              user = sr.nextInt();
          }
          // The loop terminates when amount is less than 300
    I don't understand your dilemma. The loop executes as long as the value of user is greater than or equal to 300 but terminates when the value of user is less than 300, right? What, exactly, did you expect?

    Personally, my poor little peanut brain makes the connection better if I put the variable on the left of the comparison operator and the constant on the right because that's what I am used to, but the logic is exactly the same isn't it?
        while (user >= 300) { // Same as (300 <= user)
            // whatever
        }

    Maybe if you made your program a little more verbose???
    import java.util.*;
     
    public class Z {
        public static void main(String [] args) {
     
            Scanner keyboard = new Scanner(System.in);
     
            System.out.print("Enter an integer: ");
            int amount = keyboard.nextInt();
            System.out.println("Amount = " + amount);
     
            // Loop will continue as long as amount is greater than or equal to 300
            while (amount >= 300) { // Try it with  (300 <= amount) also
                System.out.print("\nEnter another integer: ");
                amount = keyboard.nextInt();
                System.out.println("Amount = " + amount);
            }
            System.out.println("Amount is less than 300.  Goodbye for now.");
        } // End main()
    } // End class definition

    Output:

    Enter an integer: 301
    Amount = 301

    Enter another integer: 300
    Amount = 300

    Enter another integer: 299
    Amount = 299

    Amount is less than 300. Goodbye for now.


    Cheers!

    Z
    Last edited by Zaphod_b; October 2nd, 2012 at 01:05 AM.

  4. The Following User Says Thank You to Zaphod_b For This Useful Post:

    loui345 (October 2nd, 2012)

Similar Threads

  1. Quick question
    By l1nk3 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2012, 02:02 PM
  2. quick question, please HELP!
    By Nemus in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 29th, 2011, 01:23 PM
  3. Quick Question
    By sird00dguyman in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2011, 06:14 PM
  4. Another Quick Question
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 1st, 2011, 07:18 AM
  5. Hi, quick question
    By curras in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2011, 03:21 PM