Beanマシンは、三角形の形で等間隔に配置されたペグを備えた直立したボードです。ボードの開口部からボールを落とします。ボールがペグに当たるたびに、50%の確率で左または右に落下します。ボールの山は、ボードの下部にあるスロットに蓄積されます。
私は、ランダムな左または右を生成する場所にプログラムを作成しています。出力を次のような基本にします。
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