一種很簡單的遊戲,玻璃球通過下落撞擊鐵桿,會向左右兩個方向隨機變向,這樣就會有不同的結果。
1就是方向變換點,當然這個題不需要建立數組遍歷,那樣太麻煩。
package 第四次實驗;
import java.util.Scanner;
public class main737 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("請輸入球的個數:");
int num = input.nextInt();
System.out.print("請輸入機器槽數:");
int room = input.nextInt();
int [] slots = new int[room];
for(int i = 0; i < num; i++)
start(slots);
int high = 0;
for(int i = 0; i < slots.length; i++) {
if(slots[i] > high)
high = slots[i];
}
for(int i = high; i > 0; i--) {
for(int k = 0; k < slots.length; k++) {
if(slots[k] == i) {
System.out.print(" o ");
slots[k]--;
}
else
System.out.print(" ");
}
System.out.printf("\n");
}
}
public static void start(int[] slots) {
char c;
double d;
String str = "";
int num = 0;
for (int n = 0; n < slots.length - 1; n++) {
d = Math.random();
if(d>=0.5)
c='R';
else
c='L';
str += c;
if(c == 'R')
num++;
}
System.out.println(str);
slots[num]++;
}
}