Jump to content
Sign in to follow this  
Nic727

JAVA operator and result help

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?

Share this post


Link to post
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;
        }

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...