假期结束,忙里偷闲写代码!写的有点混乱··不过结果还行吧_(:з」∠)_

1 package qqqqqqqqqqqqq; 2 3 import java.util.Arrays; 4 import java.util.Scanner; 5 6 public class Qqqq { 7 8 /*豆机 9 * 提示用户输入槽数和球数 10 * 显示球的路径和落球最后的图示 11 */ 12 13 14 public static void main(String[] args) { 15 Scanner input=new Scanner(System.in); 16 System.out.println("输入球数量:"); 17 int ballNum=input.nextInt(); 18 System.out.println("输入槽数量:"); 19 int slotNum=input.nextInt(); 20 direction(slotNum,ballNum); 21 } 22 23 //循环输出左右,num1=槽数,num2=球数 24 public static void direction(int num1,int num2){ 25 int l=0; 26 int[] ballimages=new int[num1];//储存每个槽的落球数 27 System.out.println("球的路径:"); 28 while(l<num2){ 29 System.out.print("\n"+(l+1)+"号球:"); 30 int n=0; 31 for(int i=0;i<num1-1;i++){ 32 //随机数 大于5为左,反之为右 33 double f=Math.random(); 34 char d; 35 if(f>0.5) 36 {d='L';} 37 else 38 {d='R';n++;} 39 System.out.print(d); 40 } 41 ballimages[n]+=1; 42 l++; 43 } 44 //System.out.println(Arrays.toString(ballimages));//方便自己查看数组的结果,可以注释掉 45 System.out.println("\n"); 46 System.out.println("球落下图:"); 47 ball(ballimages); 48 } 49 50 //利用ballimage打印球图示 51 public static void ball(int []nums){ 52 int max=0; 53 for(int num:nums){ 54 if(num>max){ 55 max=num; 56 } 57 } 58 int i=0; 59 int j=max; 60 while(i<max){for(int num:nums){ 61 62 63 if(num>=j) 64 System.out.print("0"); 65 else 66 System.out.print(" "); 67 } 68 i++; 69 j--; 70 System.out.print("\r"); 71 } 72 } 73 74 }
本文介绍了一个使用Java编写的简单豆机模拟程序。程序通过随机数决定球的走向,并最终统计并显示各槽中球的数量分布。此外,还提供了一个可视化结果,直观展示球落入不同槽的情况。

3万+

被折叠的 条评论
为什么被折叠?



