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);
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.
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);
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.
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)