Here is what I'm suppose to accomplish:
Write a program that stimulates a bean machine Your program should prompt the user to enter the number of balls and the number of slots in the machine. Simulate the falling of each ball by printing its path.
EX.
Enter the number of balls: 5
Enter the number of slots: 7
LRLRLRL
RRLRLLL
LLRRLLR
LRLLRLR
RRRLRRL
_ _ 0
_ _ 0
0 0 0
Here is my code so far:
import javax.swing.JOptionPane;
public static void main(String[] args) {
int balls=0;
int slots=0;
char [] direction= new char [slots];
int slot=0;
int i=0;
int path=0;
balls= Integer.parseInt(JOptionPane.showInputDialog("Enter" +
" the number of balls to be dropped:"));
slots= Integer.parseInt (JOptionPane.showInputDialog("Enter " +
"the number of slots:"));
for (int j=1;j<=balls;j++){
while(i<slots){
path= (int)(Math.random()*100);
if (path <50){
direction [slots]='L';
}
else{
direction [slots]='R';
}
i++;
slot++;
}
System.out.println("The pathway is" +direction[0]+direction[1]+direction[2]+direction[3]+direction[4]);
}
}
There are a few things that I'm having problems with:
In the last line of my code where I try to print the pathway I have to basically guess the number of slots the user selected. Is there a better way to print this?
How can I print the number 'balls' that the user entered in the pattern as shown above?
Are there any other problems with my code?