0

So I'm currently trying to use an array with my program and have it add 1 every time a value is within a 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 a 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 the " + userinput + " case.\n");
        System.out.println("How many cases do you wish to open?");
        int loops = scan.nextInt();
        System.out.println("Opening " + loops + " cases!");
        for (int i = 0; i < 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 is also a final brace to close the class itself, just didn't format it on here some reason

I'm unsure where the issue even lies at this point. I'm unsure if the issue is in the array itself, or in the rest of the code. When I run this only ONE of the array groups should go up an increment. This way that if I open 10 for example, there should only be 10 total, spread throughout based on the chance. When I ran this I got many in each, eg 5 in purple, 8 in yellow, etc.

4
  • 1
    unable to understand what exactly u want to do??? Commented Oct 3, 2015 at 6:00
  • What exactly happen when you add 1 to Drops? Commented Oct 3, 2015 at 6:02
  • 1
    Where is the Drops array defined? How are your printing the array? As long as Drops is in the correct scope, and your if conditions are being met, Drops[x]++ will do what you want it to. Commented Oct 3, 2015 at 6:10
  • Using min and max the way you do makes no sense if you want values between 0 and 1. Just write double chance = rand.nextDouble(); instead. Also sometimes none of the numbers in the array will be increased. e.g. If chance is .7585 Commented Oct 3, 2015 at 6:38

3 Answers 3

1

You are initializing your array to contain the values 1 through 5:

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

You presumably want to start them at 0, which you could do explicitly as:

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

or implicitly as simply:

int Drops[5];

That should be the main cause of your issue. You also have problems in your conditions, since a value could be between the low value for one range and the high value for the next. It would be better to specify just the low value, since you are already using else if:

    if (chance >= .769)
        Drops[0] ++;
    else if (chance >= .0259)
        Drops[1] ++;
    else if (chance >= .0169)
        Drops[2] ++;
    <etc>
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Did a bit of problem solving as well as changing this and everything works great! Thanks so much :-)
0

Only one thing could be wrong; everything seeems alright.

One possibility could be that you create the Random using the same random sequence. This is a provision to test in a repeated way:

Random rand = new Random(13); // The thirteenth random sequence.

Better do

Radnom rand = new Random();

That the result is 1, 2, 3, 4 is due to the distribution of chances.

Decrease loops to have less statistical averaging.

1 Comment

If you look at the updated code you can see I did use Random rand = new Random()
0

Are you initializing your array wrong? Or printing it wrong? It seems to work fine for me.

    int[] Drops = new int[5];
    Random rand = new Random();
    int max = 10;
    int min = 5;
    for (int i = 0; i < 3; 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] ++;
    }

    for (int i=0; i<5; i++) {
        System.out.println(Drops[i]);
    }

1 Comment

Maybe it would help if I posted my whole code, I'm still unsure of what is wrong with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.