【发布时间】:2015-10-03 05:55:23
【问题描述】:
所以我目前正在尝试在我的程序中使用一个数组,并让它在每次值在设定范围内时加 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 个,等等。
【问题讨论】:
-
无法理解你到底想做什么???
-
将 1 添加到 Drops 时究竟会发生什么?
-
Drops 数组在哪里定义?您如何打印阵列?只要 Drops 在正确的范围内,并且满足您的 if 条件,
Drops[x]++就会按照您的意愿行事。 -
如果您想要介于 0 和 1 之间的值,那么使用 min 和 max 的方式是没有意义的。只需写
double chance = rand.nextDouble();代替。有时也不会增加数组中的任何数字。例如如果机会是 .7585
标签: java arrays integer increment