Jump to content

JAVA operator and result help


Nic727

Recommended Posts

Hi,

 

 

I'm making a weather app in Android studio using Java and XML for my school, and we need to use random number.

 

The thing is that it doesn't work as expected.

 

For example, I have some values here :

g1 = r.nextInt((1 + 10) + 1) - 10; //Actual temperature
        g2 = r.nextInt((-10 + 15) + 1) - 15; //minimum
        g3 = r.nextInt((5 - 2) + 1) + 2; //maximum
        g4 = r.nextInt(40) + 1; //wind
        g5 = r.nextInt((11 - 3) + 1) + 3; //sun
        g6 = r.nextInt(100); //Precipitation
        g7 = r.nextInt((30 - 1) + 1) + 1; //rain
        g8 = r.nextInt((10 - 1) + 1) + 1; //snow


//Here everything is good. But the problem is bellow.

        /***** RAIN / SNOW *****/
        if (g6 < 50) { //if precipitation is less than 50%, make rain and snow at 0
            g7 = 0;
            g8 = 0;
        }

        else if (g7 > 0) { //if it rain, make snow at 0
            g8 = 0;
        }
        else if (g8 > 0) { //if it snow, make rain at 0
            g7 = 0;
        }

        else if (g1 < 0){ //if temperature bellow 0 °C, make rain at 0
            g7=0;
        }
        else if (g1 >= 0){ //if temperature above 0, make snow at 0
            g8=0;
        }

The thing is that I can have a 90% chance of rain, but it will be at 0... Or I can have -5 °C, but it will rain instead of snow.

 

Any idea?

Link to comment
Share on other sites

Nevermind, I just found out that I just needed to change the order of my else if and now it works. I changed in this order :

if (g6 < 50) {
            g7 = 0;
            g8 = 0;
        }

        else if (g1 < 1){
            g7 = 0;
        }
        else if (g1 >= 0){
            g8 = 0;
        }

        else if (g7 > 0) {
            g8 = 0;
        }
        else if (g8 > 0) {
            g7 = 0;
        }
Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...