java中的galton box项目结果有问题
我编写了这段代码来模拟一个学校项目的高尔顿盒子: ''' 公共类GaltonBoxProject{java中的galton box项目结果有问题,java,Java,我编写了这段代码来模拟一个学校项目的高尔顿盒子: ''' 公共类GaltonBoxProject{ public static void main(String[]args){ //This will keep track of the final positions and will sort them int[] finalColumns = {0,0,0,0,0,0,0}; //will track current pos of the ball /
public static void main(String[]args){
//This will keep track of the final positions and will sort them
int[] finalColumns = {0,0,0,0,0,0,0};
//will track current pos of the ball
//8 tiers
//start from middle of the box
int randChance;
for(int a = 0; a < 100; a++) {
int currentPos = 3;
for (int i = 0; i < 9; i++) {
//resets the number
randChance = (int) (Math.random() * 100) + 1;
//if less than 50 go to the left
//if 0 --> +
//if 8 --> -
if(currentPos == 0 && randChance > 50) {
currentPos++;
}
else if (currentPos == 7 && randChance <= 50) {
currentPos--;
}
else if(currentPos != 7 && currentPos != 0 && randChance > 50){
currentPos++;
}
else if(currentPos != 7 && currentPos != 0 && randChance < 50){
currentPos--;
}
//If you want to know why I got these results
//I genuinely can't find the reason why this pattern happens
//System.out.print(" RAND: " + randChance);
//System.out.println(" POS: " + currentPos);
}
//Also debugging
//System.out.println(a + " CURRENT POS: " + currentPos);
//This will sort the numbers
for(int k = 0; k < 7; k++){
if(k == currentPos){
finalColumns[k]++;
}
}
}
//for(int i = 0; i < 8; i ++) {
// System.out.println("KEY: " + finalColumns[i]);
}
for(int k = 0; k < 7; k++){
System.out.print(k + ": ");
for(int j = 0; j < finalColumns[k]; j++){
System.out.print("*");
}
System.out.println("");
}
}
publicstaticvoidmain(字符串[]args){
//这将跟踪最终位置并对其进行排序
int[]finalColumns={0,0,0,0,0,0};
//将跟踪球的当前位置
//8层
//从盒子中间开始
内特兰德机会;
对于(int a=0;a<100;a++){
int currentPos=3;
对于(int i=0;i<9;i++){
//重置号码
randChance=(int)(Math.random()*100)+1;
//如果少于50,向左走
//如果0-->+
//如果8-->-
如果(currentPos==0&&randChance>50){
currentPos++;
}
否则如果(当前位置==7&&50){
currentPos++;
}
如果(currentPos!=7&¤tPos!=0&&randChance<50),则为else{
当前位置--;
}
//如果你想知道我为什么得到这些结果
//我真的找不到这种模式发生的原因
//系统输出打印(“兰德:”+randChance);
//系统输出打印项次(“POS:+currentPos”);
}
//也调试
//系统输出打印项次(a+“当前位置:”+currentPos);
//这将对数字进行排序
对于(int k=0;k<7;k++){
如果(k==当前位置){
最终列[k]++;
}
}
}
//对于(int i=0;i<8;i++){
//System.out.println(“键:+finalColumns[i]);
}
对于(int k=0;k<7;k++){
系统输出打印(k+“:”);
对于(int j=0;j
'''
我得到这个结果:
0:********
1:********
2:**********************
3:***
4:******************************
5:*****
6:*****************
我应该得到一个钟形曲线,我不知道为什么代码会这样做
您的代码相当长。是否需要在结尾处进行可视化?如果没有,请打印数字。请修改格式,以便人们可以阅读您的问题而不会感到头痛?首先,每行代码之间不应该有空行。您的代码相当长。是否需要在结尾处进行可视化?如果没有,只需打印数字。请修改格式,以便人们阅读您的问题时不会感到头痛。首先,每行代码之间不应该有一个空行。