JAVA
来源:互联网 发布:马东为什么离婚知乎 编辑:程序博客网 时间:2024/09/21 11:16
import java.util.Scanner;public class HelloWorld{public static void main(String[] args){char[] Dirs={'L','R'}; //定义数组确定方向int[][]showAnswer=new int[10][10];Scanner input = new Scanner(System.in);System.out.print("Enter the number of balls to drop:");int balls = input.nextInt();//balls为球的个数System.out.print("Enter the number of slots in the bean machine:");int slots = input.nextInt();//slots为钉子数System.out.println();int k,count;for (int i=0;i<balls;i++){ //每一个小球重置一次k和countk=balls-1;count=slots+1;//确定二维数组的列数,由题可知最大值即凹槽数是slots+1;for (int j=0;j<slots;j++){int dir = (int)(Math.random()*2); //产生一个0或1的随机数来决定小球运动的方向,概率均为1/2System.out.print(Dirs[dir]);//输出小球运动的方向if (dir==0)//小球向左则count--count--;else//向右则count++count++;}System.out.println();while(showAnswer[k][count/2]==1) //当底层已经有小球时则向上挪一层{k--;}showAnswer[k][count/2]=1;//确定该位置为1}for (int i=0;i<balls;i++) //当showAnswer[i][j]=1时输出0{for (int j=0;j<slots+1;j++){if (showAnswer[i][j]==1)System.out.print("O");elseSystem.out.print(" ");}System.out.println();}}}
运行结果:
1 0
- java
- JAVA
- JAVA
- JAVA
- java
- Java
- Java
- JAVA:
- java
- java
- java
- java
- Java
- java
- java
- java
- JAVA?
- java
- 在linux CentOS6上安装web环境
- 看完了linux书第二章后,关于内存寻址的一点总结
- 房地产数据处理笔记
- 庄辰超的势能,梁建章的心力
- Linux 中less的使用
- JAVA
- XML 数据岛实例
- 闲来无事写JSONのFastJson序列化
- git reset --hard回滚之后再滚回去的操作
- python zip 函数
- 客户端程序出现句柄泄露时查找办法
- 软工视频
- linux之stat命令
- 提高web应用性能之JavaScript性能调优