Can I use a Count inside of an IF statement in Java?

1522 views java
3

Im supposed to count how many times the formula in the if statement was executed but nothing shows up when I run it. Any suggestions? Here is my code:

class Main {
public static void main(String[] args) {
    double dECODING_CONSTANT = 3.452;
    double tEST_RATIO = 12;
    double aNALYSIS_BATCH_SIZE= 4;

    double bOTTLES_ON_THE_WALL = 1191;
    int count=0;
    int countDiscard=0;

    while(bOTTLES_ON_THE_WALL!=0)
    {

        if((bOTTLES_ON_THE_WALL % tEST_RATIO)>dECODING_CONSTANT)
        {
          count++;
        }
        else
        {
          countDiscard++;    
        }
    }
    System.out.println(count);
    System.out.println(countDiscard);

 }
 }

answered question

Unrelated: read about Java naming conventions. The way you use casiness for your variable names is only causing confusion here. Either it is SOME_CONSTANT or someVariable. But going lower than all upper case... dONT!

1 Answer

6

You set bOTTLES_ON_THE_WALL to 1191 and then have your loop:

while(bOTTLES_ON_THE_WALL!=0)

But you never modify the value, so it will be an infinite loop. You need to modify the value at some point in the while loop. It looks like you want to do:

bOTTLES_ON_THE_WALL = bOTTLES_ON_THE_WALL % tEST_RATIO;

At some point in your code


Also it is best to follow Java naming conventions. Use camelCase for variable names, and abbreviate when possible. So instead of

bOTTLES_ON_THE_WALL

You could have (at the very least)

bottles_On_The_Wall

Or

bottlesOnWall

posted this

Have an answer?

JD

Please login first before posting an answer.