#Generating different (random) numbers inside an array.

3 messages · Page 1 of 1 (latest)

pseudo imp
#

So, this is not an exam of mine, (proof is available if needed) since this was mentioned in the rules.

For the past day, I've been trying to solve this issue, as I'm a new student to Java, we are about to be presented to an exam in 2 - 3 days, and I'm only - currently - trying to solve those exams that are firstly produced.

For the passed exam, they (the first section) had a question of the following content:
• Make a sudoku cell, should be created by a method, the method is going to create the cell and the numbers will be printed in the main method
• All the numbers inside the cell should be randomly chosen
• The rows, columns and the cell itself can't contain the same numbers at all
• The numbers inside the cell should be from 1 to 9, and the cell should be 3 x 3

Now I've done the most part of the question (using what I've learned) and I believe the most of it isn't greatly solved but I'm willing to improve with any advice given by the respectful reader/helper.

Now my only issue is that I'm not able to change the similar numbers inside the array, I've tried everything up to my knowledge but I wasn't able to find a correct solution.

This is my code:

import java.util.Random;

public class Soduku {

    public static void main(String[] args) {
    
        sudoku();

    }
    
    static void sudoku() {

        Random rand = new Random();

        int[][] arr = new int[3][3];
    
        int i, j;
    
        for (i = 0; i < 3; i++) {

            for (j = 0; j < 3; j++) {

                arr[i][j] = rand.nextInt(9) + 1;
                System.out.print(arr[i][j]);

                if (j == 2) {
                    System.out.print("\n");


                }

            }

        }
        
    }
    
}

If I might be asked about what I've tried so far, then it's always been another for-loop where I iterate through the arr[][] looking for the similar numbers, and then assign different random numbers to them, it always came to my mind that it won't work because of multiple understandable reasons but I had no other way of solving it logically. I would appreciate any type of help, even if it's only informational

vale meadowBOT
#

Hey, @pseudo imp!
Please remember to /close this post once your question has been answered!