向数组添加增量?

时间:2015-10-03 05:55:23

标签: java arrays integer increment

所以我现在正在尝试在我的程序中使用数组,并且每次值在设定范围内时都加1。

/** 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.");
    }
}

还有一个最后的大括号来关闭这个类本身,只是没有在这里格式化它的某些原因

我不确定这个问题在哪里。我不确定问题是在数组本身,还是在代码的其余部分。 当我运行时,只有 ONE 的数组应该增加一个增量。这样,如果我打开10例如,总共应该只有10个,根据机会传播。 当我跑步时,每个都有很多,例如5个紫色,8个黄色等等。

3 个答案:

答案 0 :(得分:1)

您正在初始化数组以包​​含 1到5:

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

你可能想要在0处开始它们,你可以明确地将其作为:

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

或隐含地简单地说:

int Drops[5];

这应该是您问题的主要原因。您的条件也有问题,因为值可能介于一个范围的低值和下一个范围的高值之间。最好只指定低值,因为您已经在使用else if

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

答案 1 :(得分:0)

只有一件事可能是错的;一切都好看。

一种可能是使用相同随机序列创建Random。这是一个重复测试的条款:

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

更好

Radnom rand = new Random();

结果是1,2,3,4是由于机会的分配。

降低loops以减少统计平均值。

答案 2 :(得分:0)

您是否错误地初始化了阵列?还是打印错了?它似乎对我很好。

    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]);
    }
相关问题