Recently I've started learning Java and been having plenty of problems. I have tried searching on the website, but I can't seem to get the answer I need. Apologies if there is an answer and the mistake is on my part.
Please take a look at my code first:
import java.util.Random;
public class Test {
public static void main (String args[]) {
Random num = new Random();
int[][] multi = new int [3][];
int test = 1; // comments for stackoverflow questions
int test2 = 10; // how can i create something that will range from 1-9?
int test3 = test2-test; // trying this has always generated 0-9 instead
multi[0] = new int [] {num.nextInt(test3), num.nextInt(test3), num.nextInt(test3)};
multi[1] = new int [] {num.nextInt(test3), num.nextInt(10), num.nextInt(10)};
multi[2] = new int [] {test3, test3, num.nextInt(10)};
// PLEASE IGNORE THE 2D ARRAYS, it's just something I am testing on
int rows = 3;
int columns = 3;
for(int i=0; i<rows; i++) {
for (int j=0; j<columns; j++) {
System.out.print(multi[i][j] + "\t");
}
System.out.println();
}
}
}
I am trying to simulate a jackpot, whereby I want to generate numbers between 1 to 9 in all of the 2D array I have created. However I do not know how to generate the numbers between 1 to 9, I have creating only ranges 0-10 and 0-9
My idea is that, once I complete this 2D array of ranges 1-9, then I could use a certain amount of if-else statements to make the user win the jackpot whenever the center row or diagonal rows has 3 of the same numbers.
I have also tried using:
int test3 = num.nextInt(test2-test+1)-test;
This would generate numbers between 1-9 but if I were to use it all, it will always generate the same numbers to me, otherwise it gives an exception because of negative numbers.