gpt4 book ai didi

java - Bean Machine Galton Box Java 展示

转载 作者:行者123 更新时间:2023-11-30 11:38:34 25 4
gpt4 key购买 nike

bean 机是一个直立的板子,呈三角形均匀分布的钉子。球从开口处掉落木板。每当一个球击中一个钉子时,它就有有 50% 的几率向左或向右坠落。成堆的球堆积在板底部的插槽。

我将程序写入生成随机 Lefts 或 Rights 的位置。我希望输出是这样的基础:

Drop a ball? Yes...
LRLRLRR
Drop a ball? Yes...
RRLLLRR
Drop a ball? Yes...
LLRLLRR
Drop a ball? Yes...
RRLLLLL
Drop a ball? Yes...
LRLRRLR

0
0
0 0 0

我有它,它显示 Bean Machine 的球路径输出,但我不明白我应该如何显示具有球和槽的数组,球在槽中.. .

这是我代码的主要部分,我没有发布典型的显示方法等,因为我确定他们不会造成这个问题

    import java.util.Scanner;
import java.util.Random;

public class BeanMachine {

//constants
static final int LEFT = 0;
static final int RIGHT = 1;
static final char BALL = 'O';
//constants for options
static final int STANDARD_GAME = 0;
static final int QUIT = -1;

//Scanner for input
static Scanner keyboard = new Scanner(System.in);

//two-dimensional array to represent the bean machine
int ballsNo;
int slotsNo;
char[][]slots = new char[ballsNo][slotsNo];


//***MAIN METHOD***********************************************************

public static void main(String[] args) {
displayBanner();

int userChoice = QUIT;
boolean done = false;

System.out.print("Enter the number of balls you want to drop: ");
int ballsNo = keyboard.nextInt();

System.out.print("Enter the number of slots you want in the bean machine: ");
int slotsNo = keyboard.nextInt();



do {
displayMenu();
userChoice = getUserChoice();

switch (userChoice) {

case STANDARD_GAME:
dropBall(slotsNo);
break;
case QUIT:
done = true;
break;
default:
System.out.println("Continue playing?");
}
} while(!done);
}//end of main method

//***CLEAR SLOTS***********************************************************

public void clearSlots(int ballsNo1, int slotsNo1){
for (ballsNo = 0; ballsNo < ballsNo1; ballsNo++) {
for (ballsNo = 0; slotsNo < slotsNo1; slotsNo++) {
slots[ballsNo][slotsNo] = 0;
}
}
}

//***DROP BALL*****************************************************************

static int dropBall(int slotsNo) {


int rights = 0;
int position = 0;

while (position < slotsNo-1){
if (Math.random() < 0.5) {
System.out.println("L");
}
else{
System.out.println("R");
rights++;
}
position++;
}

return rights;

}//end of dropBall

//***DISPLAY BOARD**********************************************************

static void displayBoard(int ballsNo, int slotsNo, char [][] slots){

int count = 0;
while(count<=slotsNo){
System.out.println(slots[ballsNo][slotsNo]);
count++;
}
}



}//end of class

最佳答案

这只是正确循环数组的问题。您需要将其视为一个网格,并根据您如何看待它来确定您如何打印它:例如,假设您有一个类似 char[3][4] 的网格,您可以想象一下:

   0 1 2   <- this would be your columns
0 | | | |
1 | | | |
2 | | | |
3 |_|_|_|
^
|
this would be your rows

或者你可以这样想象它:

   0 1 2   <- this would be your columns
3 | | | |
2 | | | |
1 | | | |
0 |_|_|_|
^
|
this would be your rows

所以这取决于您认为 (0,0) 处的项目所在的位置。以下示例假定 (0,0) 位于右下角:

public class BeanMachine {

static final char BALL = 'O';
static char[][] slots;

public static void main(String[] args) {
int height = 10;
int width = 5;
slots = new char[height][width];

// just adding some stuff in here so we have something to print
slots[0][0] = BALL; // bottom left
slots[9][4] = BALL; // top right

displayBoard();
}

static void displayBoard() {
// we need to loop through each row
String columnSeperator = "|";
for (int i = slots.length - 1; i >= 0; i = i - 1) {
String row = columnSeperator;
for (int j = 0; j < slots[i].length; j = j + 1) {
// char seems to be initialized to ' ' so we do not need to do
// any checks and just add the value in the array.
row = row + slots[i][j] + columnSeperator;
}
System.out.println(row);
}
}

}

关于java - Bean Machine Galton Box Java 展示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13590414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com