java - Adding increments to an array? -


so i'm trying use array program , have add 1 every time value within set range.

/** imports **/ import java.util.scanner; import java.util.random;  /** main code **/ public class droprate2 { public static void main(string[] args) {     double min, max;     min = 0;     max = 1;     scanner scan = new scanner(system.in);     system.out.println("enter case type:");     string userinput = scan.nextline();     random rand = new random();     int drops[] = {1, 2, 3, 4, 5};      /** shadow **/     if (userinput.equalsignorecase("shadow"))     {         system.out.println("you chose " + userinput + " case.\n");         system.out.println("how many cases wish open?");         int loops = scan.nextint();         system.out.println("opening " + loops + " cases!");         (int = 0; < loops; i++)         {             double chance = min + (max - min) * rand.nextdouble();             if (chance >= .769)                 drops[0] ++;             else if (chance >= .0259 && chance <= .758)                 drops[1] ++;             else if (chance >= .0169 && chance <= .258)                 drops[2] ++;             else if (chance >= .0089 && chance <= .0168)                 drops[3] ++;             else if (chance >= 0 && chance <= .0088)                 drops[4] ++;         }         system.out.println("you got " + drops[0] + " blues.");         system.out.println("you got " + drops[1] + " purples.");         system.out.println("you got " + drops[2] + " pinks.");         system.out.println("you got " + drops[3] + " reds.");         system.out.println("you got " + drops[4] + " yellows.");     } } 

there final brace close class itself, didn't format on here reason

i'm unsure issue lies @ point. i'm unsure if issue in array itself, or in rest of code. when run one of array groups should go increment. way if open 10 example, there should 10 total, spread throughout based on chance. when ran got many in each, eg 5 in purple, 8 in yellow, etc.

you initializing array contain values 1 through 5:

int drops[] = {1, 2, 3, 4, 5}; 

you presumably want start them @ 0, explicitly as:

int drops[] = {0, 0, 0, 0, 0}; 

or implicitly simply:

int drops[5]; 

that should main cause of issue. have problems in conditions, since value between low value 1 range , high value next. better specify low value, since using else if:

    if (chance >= .769)         drops[0] ++;     else if (chance >= .0259)         drops[1] ++;     else if (chance >= .0169)         drops[2] ++;     <etc> 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -