PadmaJS

Exercise 6.21

Sep 30th, 2022
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.10 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Exercise6_21 {
  4.   public static void main(String[] args) {
  5.     Scanner in = new Scanner(System.in);
  6.     System.out.print("Enter the number of balls to drop: ");
  7.     int balls = in.nextInt();
  8.     System.out.print("Enter the number of slots in the bean machine: ");
  9.     int slotNum = in.nextInt();
  10.     int[] slots = new int[slotNum];
  11.     for (int i = 0; i < balls; i++) {
  12.       int count = slotNum - 1;
  13.       int rights = 0;
  14.       String path = "";
  15.       while (count > 0) {
  16.         double choice = Math.floor(Math.random() * 2);
  17.         if (choice == 0) {
  18.           path += "L";
  19.         } else {
  20.           path += "R";
  21.           rights++;
  22.         }
  23.         count--;
  24.       }
  25.       slots[rights]++;
  26.       System.out.println(path);
  27.     }
  28.     while (balls > 0) {
  29.       String ballPlace = "";
  30.       for (int i = 0; i < slots.length; i++) {
  31.         if (slots[i] == balls) {
  32.           ballPlace += "O";
  33.           slots[i]--;
  34.         } else {
  35.           ballPlace += " ";
  36.         }
  37.       }
  38.       balls--;
  39.       System.out.println(ballPlace);
  40.     }
  41.   }
  42. }
  43.  
Add Comment
Please, Sign In to add comment