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: the "WHILE"

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Mumbai
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb the "WHILE"

    The problem is it is still giving the output as 3 sometimes which should not be.
    Kindly help me with this snippet
    //CODE SNIPPET
    int number1=4;
    while(number1>3)
    {
    number1=(int)(Math.random()*10);
    }
    System.out.print(number1);

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,152
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: the "WHILE"

    Can you show what the code currently does and explain what is wrong with its output and show what you want the output to be.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: the "WHILE"

    So, this is how I read your snippet.

    variable number1 equals 4
    while number1 is greater than 3
    number1 is a random double from 0 to 1 multiplied by 10 and converted to an int.
    display number1

    from what you are saying, you are not expecting the value 3 to show up.
    so what happens after number1 is set to 3 by the random equation?

    This link may help you find the answer:
    Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

Similar Threads

  1. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM