Note that there are some explanatory texts on larger screens.

plurals
  1. POBean Machine Galton Box Java Display
    text
    copied!<p>A bean machine is an upright board with evenly spaced pegs in a triangular form. Balls are dropped from the opening of the board. Every time a ball hits a peg, it has a 50% chance of falling to the left or right. Piles of balls are accumulated in the slots at the bottom of the board.</p> <p>I have the program written to where it generates the random Lefts or Rights. I want the output to be the basics of the like:</p> <pre><code>Drop a ball? Yes... LRLRLRR Drop a ball? Yes... RRLLLRR Drop a ball? Yes... LLRLLRR Drop a ball? Yes... RRLLLLL Drop a ball? Yes... LRLRRLR 0 0 0 0 0 </code></pre> <p>I have it where it displays output of the path of the ball for the Bean Machine, but I don't understand how I'm supposed to display the array that has the balls and slots with the balls in the slots...</p> <p>Here is the main parts of my code, I'm not posting the typical display methods and the such because i'm sure they are not making this problem</p> <pre><code> import java.util.Scanner; import java.util.Random; public class BeanMachine { //constants static final int LEFT = 0; static final int RIGHT = 1; static final char BALL = 'O'; //constants for options static final int STANDARD_GAME = 0; static final int QUIT = -1; //Scanner for input static Scanner keyboard = new Scanner(System.in); //two-dimensional array to represent the bean machine int ballsNo; int slotsNo; char[][]slots = new char[ballsNo][slotsNo]; //***MAIN METHOD*********************************************************** public static void main(String[] args) { displayBanner(); int userChoice = QUIT; boolean done = false; System.out.print("Enter the number of balls you want to drop: "); int ballsNo = keyboard.nextInt(); System.out.print("Enter the number of slots you want in the bean machine: "); int slotsNo = keyboard.nextInt(); do { displayMenu(); userChoice = getUserChoice(); switch (userChoice) { case STANDARD_GAME: dropBall(slotsNo); break; case QUIT: done = true; break; default: System.out.println("Continue playing?"); } } while(!done); }//end of main method //***CLEAR SLOTS*********************************************************** public void clearSlots(int ballsNo1, int slotsNo1){ for (ballsNo = 0; ballsNo &lt; ballsNo1; ballsNo++) { for (ballsNo = 0; slotsNo &lt; slotsNo1; slotsNo++) { slots[ballsNo][slotsNo] = 0; } } } //***DROP BALL***************************************************************** static int dropBall(int slotsNo) { int rights = 0; int position = 0; while (position &lt; slotsNo-1){ if (Math.random() &lt; 0.5) { System.out.println("L"); } else{ System.out.println("R"); rights++; } position++; } return rights; }//end of dropBall //***DISPLAY BOARD********************************************************** static void displayBoard(int ballsNo, int slotsNo, char [][] slots){ int count = 0; while(count&lt;=slotsNo){ System.out.println(slots[ballsNo][slotsNo]); count++; } } }//end of class </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload