Project Requirements
Input the number of balls and the number of slots, beans in the nail according to the first few lines have several nails arranged, the number of rows (Groove-1).
The rolling direction of each ball is printed out and the distribution of the balls in each slot is shown.
The code is as follows
Import Java.util.Arrays;
Import Java.util.Scanner;
public class Beanmachine {static Scanner input = new Scanner (system.in);
public static void Main (string[] args) {System.out.println ("Enter the number of balls to drop:");
int beannums = Input.nextint ();
System.out.println ("Enter the number of slots in the Bean Machine:");
int slots = Input.nextint ();
Showanswer (beannums, slots);
public static void Showanswer (int beannums, int slots) {int direction;
int[] Dir = new Int[beannums];
Int[][] slo = new Int[slots][beannums];
A ball roll down for (int i = 0; i < beannums ++i) {//Nail line number is less than 1 for (int j = 0; j < slots-1; ++j) {
Direction indicates that the ball is left or right, taking a random number of 0 or 1 direction = (int) (Math.random () * 2); When the direction is 0 o'clock, remember to turn left;
Dir[i] for the first small ball rolling process several times to the right roll.
if (direction = = 0) {System.out.print ("L");
else {System.out.print ("R");
dir[i]++;
} System.out.println (); for (int j = 0; J < Slots -1;
++J) {//slo[j][count] indicates how many balls int count = BeanNums-1 are the number of J slots from bottom to top;
for (int i = 0; i < beannums ++i) {if (dir[i] = = j) {Slo[j][count] = 1;
count--; }}///show slots for (int i = 0; i < beannums ++i) {//nail row count less than 1 for (int j = 0; j < slots-1;
++J) {if (slo[j][i] = = 1) {System.out.print ("0");
else {System.out.print ("");
} System.out.println ();
}
}
}
Run Results
Enter the number of balls to drop: 5 Enter the number of slots in the
Bean machine:
8
Lrrrllr
lllrllr< C6/>LLRRLRR
rrlrlll
lllrrrr
0
0
000
The topic comes from the Java language Programming program (p230-6.21***)