我需要在" for"中制作一个随机发生器。循环将运行100,1,000,000和1,000,000,000次。这个想法是模拟轮盘赌轮多次旋转,然后计算:
我已经编写了一个if/else if
语句来为数字0-36分配一个颜色,并且编写了for
循环,其中包含一个随机生成器和一个颜色的吸气剂(我还没想到)究竟我将如何计算重复数字的字符串)。我的循环应该重复100次,根据我读过的所有内容,它应该重复。但是,当它打印时,它只打印47行。这让我相信我的循环只是初始化了47次。如果是这种情况,我做错了什么?
import java.util.Random;
public class RouletteGenerator
{
public static void main(String[] args) {
RouletteWheel wheelGenerator = new RouletteWheel();
Random generator = new Random();
//FIELDS
int random;
int red = 0;
int black = 0;
int two = 0;
int three = 0;
int even = 0;
int odd = 0;
int lastNumber = 0;
int lastNumber2 = 0;
for (int i = 0; i <= 100; i++) {
random = generator.nextInt(37);
wheelGenerator.setNumber(random);
String numberColor = wheelGenerator.getColor();
System.out.println("Your random number is: " + random + " - " + numberColor);
}
}
}
这是我的另一个类的代码,它为每个随机数提供基于实际轮盘赌轮的颜色。
public class RouletteWheel
{
/**
* FIELDS
*/
int rouletteNumber;
/**
* DEFAULT CONSTRUCTOR
*/
RouletteWheel() {
rouletteNumber = 0;
}
/**
* SET THE NUMBER FIELD (SETTER)
*/
void setNumber(int numberIn) {
rouletteNumber = numberIn;
}
/**
* GETS THE NUMBER'S COLOR
*/
String getColor() {
if(rouletteNumber ==0) {
return "Green";
} else if(rouletteNumber % 2 == 0) {
//EVEN NUMBERS
if(rouletteNumber <= 10) {
//1-10
return "Black";
} else if(rouletteNumber <= 18) {
//11-18
return "Red";
} else if(rouletteNumber <= 28) {
//19-28
return "Black";
} else {
//29-36
return "Red";
}
} else {
//ODD NUMBERS
if(rouletteNumber >= 29) {
//29-36
return "Black";
} else if(rouletteNumber >= 19) {
//19-28
return "Red";
} else if(rouletteNumber >= 11) {
//11-18
return "Black";
} else {
//1-10
return "Red";
}
}
}
}