2
import java.util.Random;
public class RandomHomework
{
    public static void main(String[] args)
    {
        int i;
        Random generator = new Random();
        double randomDecimals = generator.nextDouble()-.04;
        int randomNumber = generator.nextInt(9)+10;
        for(i = 0; i > 100; i++)
        {
            if(randomNumber >= 10.00)
            {
                System.out.println(randomNumber + randomDecimals);
            }
        }
    }
}

I am having a problem with the setup of my for loop and cannot figure it out... It runs perfectly fine when I remove the for loop.

As you can see I tried declaring the i previously but it made no difference.

6
  • 1
    Please post your code as an MCVE and not as an external link. Commented Sep 9, 2014 at 17:11
  • 2
    Switch > with < in your loop condition. Commented Sep 9, 2014 at 17:11
  • 1
    Your condition is wrng Commented Sep 9, 2014 at 17:12
  • 2
    When is 0 > 100 true? Commented Sep 9, 2014 at 17:20
  • After you fix your for loop, your program still won't work since it will either skip over printing the randomNumber 100 times, or print the same randomNumber 100 times. You presumably wanted to generate a new random number inside the loop body? Commented Sep 9, 2014 at 18:27

6 Answers 6

12
for(i = 0; i > 100; i++)

This says: start with i set to zero and continue as long as it is greater than 100.

This stops right away

Sign up to request clarification or add additional context in comments.

Comments

3

The problem is the condition of the loop

for(i = 0; i > 100; i++)

The condition should be i < 100

Comments

1

Your loop condition is always false. You start from i = 0 and say run while i > 100. However, 0 is never > 100 so your loop never happens.

Change

for(i = 0; i > 100; i++)

To

for(i = 0; i < 100; i++)

Comments

1

You must change:

for(i = 0; i > 100; i++)

to:

for(i = 0; i < 100; i++)

for the loop to execute.

Comments

0

You should use

for(i = 0; i < 100; i++)

instead of

for(i = 0; i > 100; i++)

else it would end as soon as it starts since you are checking i > 100

Comments

-1

You need to use for(i =0; i<100; i++) cuz your version is ended right away

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.