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?
-
Well, for starters, I'm getting a consistent
ArrayIndexOutOfBoundsException
on the linedirection[slots] = 'L';
(or'R'
). That's becausedirection
is always of length 0, since you initialized it toslots
whenslots
was 0. Move the linechar [] direction= new char [slots];
to after
slots
is input.Next, you always assign the 'L' or 'R' to the position immediately after the end of the array. That's another reason for the
ArrayIndexOutOfBoundsException
I was getting. Change the assignment todirection[i] = 'L'; // or 'R'
Next, you don't reset
i
after thewhile
loop. So the path is calculated only for the first ball and then reused for all the others. I would make it afor
loop instead, like this:for (i = 0; i < slots; i++) { // your code here (make sure you don't change i inside the loop) }
Finally, as others have said, you should be using a loop to print out the path. You know how long the
direction
array is (it'sdirection.length
, if you didn't know), so you can just loop through it and print out each letter.Once you've made these changes, your program should work (edit: except that it doesn't keep track of which slot each ball ends up in). It will still have some room for improvement, but finding those things is part of the fun--isn't it?
에이바 : Thanks for your help I'm working on fixing it right now. -
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?
Use a for loop, and
System.out.print()
so you don't get a new line after each step.How can I print the number 'balls' that the user entered in the pattern as shown above?
For each slot, you need to record the number of balls which ended up in that slot, and the maximum for any slot. Given these two values, you can loop over each slot, and print '_' or '0' the appropriate number of times with a nested loop.
Are there any other problems with my code?
You only appear to be printing the path of the last ball dropped, rather than each ball, but that might just be your indentation being squiffy. Post properly formatted, complete code.
The example output you have seems to be reading input from the console, not using swing.
You're declaring variables too early. It's better to declared variables on first use, and if they don't change mark them
final
. If you had:final int slots= Integer.parseInt (...
instead of
int slots = 0; ... slots= Integer.parseInt (...
then at least one of your bugs would be caught by the compiler.
-
Some answers:
- Think
for loop
again. System.out.print or System.out.printf will help. - System.out.println("Number of balls: "+ something that makes an int into a string (what would that be?)
Why are you putting the result of Math.random() into an int? What would happen if you used:
if(Math.random() < 0.5) { // do something
} else { // do the opposite
}
- Think