第七章第三十七题(游戏:豆机)(Game: bean machine)

第七章第三十七题(游戏:豆机)(Game: bean machine)

  • ***7.37(游戏:豆机)豆机,也称梅花瓶或高尔顿瓶,他是一个用来做统计实验的设备,以英国科学家弗兰克斯·高尔顿的名字来命名。他是一个三角形状的均匀防止钉子(或钩子)的直立板子。球从板子开口落下。每当球碰到钉子,他以50%的概率落向左边或者落向右边。在板子底部的各个槽中都会堆积一堆球。
    编写程序模拟豆机。程序应该提示用户输入球的个数以及机器的槽数。打印每个球的路径模拟他的下落。使用条形图显示槽中球的最终储备量。下面时程序的一个运行示例:

    请输入球的个数:
    5
    请输入机器槽数:
    8
    RRRLLLL
    RRRLRRR
    LRLRLRL
    RRLRLLL
    RLLRLRR
    | | | | o | | | | |
    | | | | o | | | | |
    | | | | o | o | | o | |

    ***7.37(Game: bean machine)Bean machine, also known as may vase or Galton bottle, is a device for statistical experiments, named after British scientist Franks Galton. It is a triangular shaped upright board that evenly prevents nails (or hooks). The ball fell through the opening of the board. Every time he hits the ball on the left, or on the right. There is a pile of balls in each slot at the bottom of the board.
    Write program to simulate bean machine. The program should prompt the user to enter the number of balls and the number of slots in the machine. Print the path of each ball to simulate his fall. Use a bar chart to show the final reserve of balls in the slot. The following is a running example of the program:

    请输入球的个数:
    5
    请输入机器槽数:
    8
    RRRLLLL
    RRRLRRR
    LRLRLRL
    RRLRLLL
    RLLRLRR
    | | | | o | | | | |
    | | | | o | | | | |
    | | | | o | o | | o | |

  • 参考代码:

    package chapter07;
    
    import java.util.Scanner;
    
    public class Code_37 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("请输入球的个数:");
            int numBall = input.nextInt();
            System.out.println("请输入机器槽数:");
            int numRoom = input.nextInt();
            int[] slots = new int[numRoom];
    
            for(int i = 0; i < numBall; i++)
                run(slots);
    
            int max = max(slots);
    
            display(max, slots);
        }
    
        /*球的路径*/
        public static void run(int[] slots) {
            char i = Math.random() >= 0.5 ? 'R' : 'L';
            String s = "";
            int num = 0;
            for (int n = 0; n < slots.length - 1; n++) {
                i = Math.random() >= 0.5 ? 'R' : 'L';
                s = s + i;
                if(i == 'R')
                    num++;
            }
            System.out.println(s);
            slots[num]++;
        }
    
        /*槽里最多多少个球*/
        public static int max(int[] slots) {
            int max = 0;
            for(int i = 0; i < slots.length; i++) {
                if(slots[i] > max)
                    max = slots[i];
            }
            return max;
        }
    
        /*打印条形图*/
        public static void display(int max, int[] slots) {
            for(int i = max; 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.print("|");
                System.out.println("");
            }
        }
    }
    
    
  • 结果显示:

    请输入球的个数:
    5
    请输入机器槽数:
    8
    RRRLLLL
    RRRLRRR
    LRLRLRL
    RRLRLLL
    RLLRLRR
    |   |   |   | o |   |   |   |   |
    |   |   |   | o |   |   |   |   |
    |   |   |   | o | o |   | o |   |
    
    Process finished with exit code 0
    
    
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值