Генерация проверки на магический квадрат

Я пытаюсь получить магический квадрат, который складывается по горизонтали, вертикали и диагонали к одному и тому же числу. Это то, чего пытается достичь мое окончание или условие победы.

У меня ощущение, что у меня слишком много инициализированных значений, начиная с int tableadd (ближе к концу для уточнения).

Примером победных условий будет как таковой.

4 9 2
3 5 7
8 1 6

Как таковые вертикальные, горизонтальные и диагональные добавить к 15.

Вот мой код

   public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        int[][] table;
        boolean stopasking = true;
        boolean win = false;
        int size = 0;
        int row = 0;
        int column = 0;
        int value = 0;
        while (stopasking == true) 
        {
            System.out.print("Let's make a magic Square! How big should it be? ");
            size = input.nextInt();
            if (size >= 9) 
            {
                System.out.println("That's huge! Please enter a number less than 9.");
                System.out.println("");
            } else if (size <= 2) 
            {
                System.out.println("That would violate the laws of mathematics!");
                System.out.println("");
            } else 
            {
                stopasking = false;
            }
        }
        table = new int[size][size];
        int max = (size * size);
        stopasking = true;
        while (stopasking == true) 
        {
            System.out.println("The square currently looks like this: ");
            for (int i = 0; i < table.length; i++) 
            {
                for (int j = 0; j < table.length; j++) 
                {
                    System.out.print(table[i][j] + " ");
                }
                System.out.println("");
            }

            System.out.println("Where do you want to put a new value?");
            System.out.print("Row : ");
            row = input.nextInt();
            System.out.print("Column: ");
            column = input.nextInt();
            System.out.print("What value should go there? ");
            value = input.nextInt();
            System.out.println("");
            if (value > max || value < 1) 
            {
                System.out.println("You can only use numbers between 1 and " + max + " for this square.");
            } else 
            {
                boolean check = true;
                for (int i = 0; i < table.length; i++) 
                {
                    for (int j = 0; j < table.length; j++) 
                    {
                        if (table[i][j] == value) 
                        {
                            System.out.println("This value is already in use.");
                            check = false;
                        }
                    }
                }
                if (check == true) 
                {
                    table[row][column] = value;
                }
            }
        }
        int tableadd = 0;
        int[] totalvalues = new int[size];
        for (int i = 0; i < table.length; i++) 
        {
            value += totalvalues[i];          
        }
        tableadd++;
        if(totalvalues == totalvalues)
        {
            System.out.println("Victory!");   
            win = true;
        }
    }
}

0 ответов

Другие вопросы по тегам