禁用数组中的数字重复

Disable duplication of numbers in an array

我设法使程序运行,但问题出在第 3 行第 2 列和第 3 行第 3 列之间。每当我在第 3 行第 2 列和第 3 行第 3 列中输入两个重复数字时,它会接受这两个数字。 如果我在那里输入 1,它会循环回来。我唯一的问题是数字 8。为什么?

这是我的代码:

Scanner input = new Scanner(System.in);
    int magic_square[][]=new int[3][3];
    int counter = 0;
    int valid = 0;
    int duplicate = 0;
    int sum_row[] = new int[3];
    int sum_col[] = new int[3];
    int rowcolcheck =0;
    int rowcheck=0;

    System.out.println("Enter the Magic Number: ");
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            valid = 0;
            do{
                duplicate = 0;
                System.out.print("Row "+(i+1)+" Column "+(j+1)+": ");
                magic_square[i][j] = input.nextInt();
                if(magic_square[i][j]<0||magic_square[i][j]>9){
                    System.out.println("Invalid input range! Please try again!");
                    valid = 1;
                    continue;
                }

                valid = 0;
                for(int a=0;a<3;a++){
                    for(int b=0;b<3;b++){
                        if(a==i&&b==j)
                        {
                            continue;
                        }
                        if(magic_square[i][j]==magic_square[a][b]){
                            duplicate = 1;
                            System.out.println("Duplicate Number! Please try again!");
                            continue;
                        }
                        if(duplicate==1){
                            valid = 1;
                        }

                    }
                }
            }while(valid!=0);
        }
    }

我修好了!

 valid = 0;
            for(int a=0;a<3;a++){
                for(int b=0;b<3;b++){
                    if(a==i&&b==j)
                    {
                        continue;
                    }
                    if(magic_square[i][j]==magic_square[a][b]){
                        valid = 1; // i change this. it was duplicate = 1. 
                                   //now it's valid = 1.
                        System.out.println("Duplicate Number! Please try again!");
                        continue;
                    }
                      //i removed this conditon and the integer duplicate =
                      //                      0 and it works like a charm!
                    //if(duplicate==1){
                      //  valid = 1;
                    //}

                }
            }