Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Array help

dextercdexterc Member Posts: 1
Hello, I am trying to create a method that will out put 3 random variables consisting of either 7, Cherry, Space, or Bar (it's part of a slot machine). I am new to arrays and I am not sure out to put an out put of an operation and put it inside an array. The problem I am having is at the bottom of my code.
[code]
String[] total = new String[3];
String var = "0";
Random rand = new Random();
for (int j=0; j < 3; j++)
{
int pickedNumber = rand.nextInt(8) + 1;
if (pickedNumber == 1)
{
var = "7";
}
else if (pickedNumber == 2)
{
var = "Cherry";
}
else if (pickedNumber == 3 || pickedNumber == 4)
{
var = "Space";
}
else if (pickedNumber == 5 || pickedNumber == 6 || pickedNumber == 7
|| pickedNumber == 8)
{
var = "Bar";
}
System.out.print(var + " ");
total[3] += var;
System.out.println(total[0]);
}
}
}
[/code]

It returns with a run time error, it appears that the var is being put into the array.

Any help would be greatly appreciated!
-Dexter

Comments

  • skaa7skaa7 Member Posts: 33
    [b]total[/b] array has dimension = 3, so you can use [b]total[0][/b], [b]total[1][/b], and [b]total[2][/b] elements only!
Sign In or Register to comment.