java 练习——豆机(java)

一种很简单的游戏,玻璃球通过下落撞击铁杆,会向左右两个方向随机变向,这样就会有不同的结果。

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]++;
    }
}
发布了77 篇原创文章 · 获赞 7 · 访问量 9076

猜你喜欢

转载自blog.csdn.net/qq_41886231/article/details/101457019
今日推荐