【Java】豆机

来源:互联网 发布:windows不激活能用吗 编辑:程序博客网 时间:2024/06/10 19:13



import javax.swing.JOptionPane;public class BeanMachine6_21 { public static void main(String[] args) {    String BallStr=JOptionPane.showInputDialog("请输入球的个数:");  int Ball=Integer.parseInt(BallStr);//球数    String SlotStr=JOptionPane.showInputDialog("请输入豆机的槽数:");  int Slot=Integer.parseInt(SlotStr);//槽数    char [] deriction={'L','R'};//方向  int [] slots=new int[Slot];//对应槽中的槽数          for(int k=0;k<Slot;k++){      slots[k]=0;  }    String Path="The path: "+"\n";    for(int j=0;j<Ball;j++){//下落的路径   int count=0;  for(int i=0;i<Slot;i++){      int index=(int)(Math.random()*2);      if(index==1){    count++;   }   Path=Path+deriction[index];  }      Path=Path+"\n";      slots[count]=slots[count]+1;  }     String Message="";              for(int k=0;k<Slot;k++){        Message=Message+slots[k]+":";       for(int i=0;i<slots[k];i++){        Message=Message+"●";       }       Message=Message+"\n";      }                       JOptionPane.showMessageDialog(null, Path+Message); }}


11 0
原创粉丝点击