HW6.21

HW6.21

HW6.21

 import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);
         System.out.print("Enter the number of balls to drop: ");
         int numberOfBalls = input.nextInt();
         System.out.print("Enter the number of slots in the bean machine: ");
         int numberOfSlots = input.nextInt();

         input.close();

         int[] slots = new int[numberOfSlots];
         int randomNumber;
         String direction;
         int count;

         for(int j = 0; j < numberOfBalls; j++)
         {
             count = 0;
             for(int i = 0; i < numberOfSlots; i++)
             {
                 randomNumber = (int)(Math.random() * 10);
                 if(randomNumber >= 5)
                 {
                     direction = "R";
                     count++;
                 }
                 else
                     direction = "L";
                 System.out.print(direction);
             }
             slots[count]++;
             System.out.println();
         }

         for(int i = 0; i < numberOfSlots; i++)
             System.out.print(slots[i] + " ");

         String[][] outcome = new String[numberOfBalls][numberOfSlots];

         for(int i = 0; i < numberOfBalls; i++)
             for(int j = 0; j < numberOfSlots; j++)
                 outcome[i][j] = " ";

         for(int i = 0; i < numberOfSlots; i++)
         {
             for(int j = numberOfBalls - 1; j > numberOfBalls - slots[i]; j--)
                 outcome[j][i] = "O";
         }

         for(int i = 0; i < numberOfBalls; i++)
         {
             for(int j = 0; j < numberOfSlots; j++)
                 System.out.print(outcome[i][j]);
             System.out.println();
         }
     }
 }
上一篇:package-lock.json和package.json的作用


下一篇:GOF23设计模式之单例模式