bingo ticket loop problem (doesn't compile like it should...)
Code Java:
import java.util.Scanner;
import java.util.Random;
public class demo
{
public static void main(String[] args)
{
Random rand= new Random(); // being able to use random object
int[][] array = new int[3][9];
// print array in rectangular form
for (int i=0; i<array.length; i++)
{
for (int j=0; j<array[i].length; j++)
{
for(int x=0;x<5;x++)
{
int k = rand.nextInt(9);
array[i][k] = k*10 +rand.nextInt(9)+1;
}
System.out.print("\t" + array[i][j]);
}
System.out.println("");//
}
}
}
I'm creating a bingo ticket. I need to respect these rules
-
The first column contains numbers from 1 to 9,
- The second column numbers from 10 to 19,
- The third 20 to 29 and so on up until the last column, which contains numbers from 80 to 90 (the 90 being
placed in this column as well)
- Each row contains five numbers and four blank spaces
I'm having a problem with the 5 numbers max per row, i created the array[3][9], then i made a loop so it would put 5 numbers in a random place in the row but the output show me that there is a number in everywhere ...
Did i made a mistake somewhere ?
thank you
Re: bingo ticket loop problem (doesn't compile like it should...)
You say this doesn't compile, but you haven't provided a compiler error. What error are you getting?
Re: bingo ticket loop problem (doesn't compile like it should...)
oh sorry by doesnt compile; i wanted to say it doesnt give the same output when i tried the loop
for(int x=0;x<5;x++)
{
int k = rand.nextInt(9);
array[i][k] = k*10 +rand.nextInt(9)+1;
}
in a different class to test it out . It supposed to create 5 (x<5) numbers but it create 27 in total for all the arrays !
Re: bingo ticket loop problem (doesn't compile like it should...)
0 0 0 34 45 52 62 77 87
0 0 24 32 43 58 66 73 87
0 16 21 32 46 54 65 77 86
it gave me this but it supposed to have 5 random number per row ...
Re: bingo ticket loop problem (doesn't compile like it should...)
I'm really not sure what you're asking. Your array is 3 X 9, which means it has 27 elements. That for loop doesn't really do much, as it's setting the same element each time, so only the last iteration matters.
Re: bingo ticket loop problem (doesn't compile like it should...)
if i understand correctly if i delete the loop for(int x=0;x<5;x++)... it will only create only 0 in every elements
i thought that by making the last loop it will only create a random number in a random spot on the row until there is 5 number then start over on the next line and the others element will be 0, dont know if im making any sense...
i'm doing my first java programming class so i'm still a beginner in java
Re: bingo ticket loop problem (doesn't compile like it should...)
Quote:
Originally Posted by
mv740
if i understand correctly if i delete the loop for(int x=0;x<5;x++)... it will only create only 0 in every elements
What happened when you tested that theory?
Quote:
Originally Posted by
mv740
i thought that by making the last loop it will only create a random number in a random spot on the row until there is 5 number then start over on the next line and the others element will be 0, dont know if im making any sense...
i'm doing my first java programming class so i'm still a beginner in java
Why do you want to randomize the spot? You can't guarantee you hit every spot- you might hit the same spot several times. Why not just iterate through each element and set it equal to whatever you want?