首页 > 代码库 > HW6.21
HW6.21
1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 System.out.print("Enter the number of balls to drop: "); 9 int numberOfBalls = input.nextInt();10 System.out.print("Enter the number of slots in the bean machine: ");11 int numberOfSlots = input.nextInt();12 13 input.close();14 15 int[] slots = new int[numberOfSlots];16 int randomNumber;17 String direction;18 int count;19 20 for(int j = 0; j < numberOfBalls; j++)21 {22 count = 0;23 for(int i = 0; i < numberOfSlots; i++)24 { 25 randomNumber = (int)(Math.random() * 10);26 if(randomNumber >= 5)27 {28 direction = "R";29 count++;30 }31 else32 direction = "L";33 System.out.print(direction);34 }35 slots[count]++;36 System.out.println();37 }38 39 for(int i = 0; i < numberOfSlots; i++)40 System.out.print(slots[i] + " ");41 42 String[][] outcome = new String[numberOfBalls][numberOfSlots];43 44 for(int i = 0; i < numberOfBalls; i++)45 for(int j = 0; j < numberOfSlots; j++)46 outcome[i][j] = " ";47 48 for(int i = 0; i < numberOfSlots; i++)49 {50 for(int j = numberOfBalls - 1; j > numberOfBalls - slots[i]; j--)51 outcome[j][i] = "O";52 }53 54 for(int i = 0; i < numberOfBalls; i++)55 {56 for(int j = 0; j < numberOfSlots; j++)57 System.out.print(outcome[i][j]);58 System.out.println();59 }60 }61 }
HW6.21
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。