Jump to content


Photo

JAVA operator and result help


  • Please log in to reply
1 reply to this topic

#1 Nic727

Nic727

    Member

  • Members
  • PipPip
  • 195 posts

Posted 12 December 2016 - 01:54 PM

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?



#2 Nic727

Nic727

    Member

  • Members
  • PipPip
  • 195 posts

Posted 12 December 2016 - 03:32 PM

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;
        }





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users