如何检查多个2d数组的所有值是否全部相同?

时间:2021-10-03 10:03:45

my main problem is the program mustn't allow the user to input only the same digit during all the times the program asks for a value.

我的主要问题是程序在程序要求输入值的所有时间内都不允许用户只输入相同的数字。

for example, the user needs to input values 9 times, and the user only inputs "1" as a value 9 times, the program must take this as an error, meaning there should be variety.

例如,用户需要输入值9次,而用户只输入“1”作为值9次,程序必须将此作为错误,这意味着应该有多种。

although, the user can still input the same values sometimes, but not ALL THE TIME. The user can enter "1" 2x, 3x, 4x, etc, but not 9x.

尽管如此,用户有时仍然可以输入相同的值,但不是所有时间。用户可以输入“1”2x,3x,4x等,但不能输入9x。

for example:

these can be allowed:

这些可以被允许:

enter digit 1: 1
enter digit 2: 2
enter digit 3: 1
enter digit 4: 1
enter digit 5: 3
enter digit 6: 5
enter digit 7: 5
enter digit 8: 5
enter digit 9: 5

输入数字1:1输入数字2:2输入数字3:1输入数字4:1输入数字5:3输入数字6:5输入数字7:5输入数字8:5输入数字9:5

however, these can't be allowed: enter digit 1: 1
enter digit 2: 1
enter digit 3: 1
enter digit 4: 1
enter digit 5: 1
enter digit 6: 1
enter digit 7: 1
enter digit 8: 1
enter digit 9: 1

但是,这些不允许:输入数字1:1输入数字2:1输入数字3:1输入数字4:1输入数字5:1输入数字6:1输入数字7:1输入数字8:1输入数字9:1

            int [][]array=new int [aa][aa];

for the inputting part:

输入部分:

        for (row=0;row<aa;row++)
            {
                for (col=0;col<aa;col++)
                {
                    System.out.print("Enter [" + row + "][" + col + "] = ");
                    array[row][col]=input.nextInt();
                }
            }


proceeding after this, the checking if all entered values are all the same should be performed

在此之后,应检查是否所有输入的值都相同

3 个解决方案

#1


1  

    Scanner input = new Scanner(System.in);
    int arr[] = new int[9];
    arr[0] = input.nextInt();
    boolean error = true;
    for (int i=1; i<9 ; i++)
    {
        arr[i] = input.nextInt();
        if (arr[i]!=arr[0])
            error = false;
    }

    if (error)
        System.out.println("Invalid");
    else
        System.out.println("valid");

#2


1  

Put the first input into a variable. If one input is not equal to the variable, you have a valid input. Otherwise you can reject it.

将第一个输入放入变量中。如果一个输入不等于变量,则输入有效。否则你可以拒绝它。

Consider the following input, which is invalid

请考虑以下输入,该输入无效

11111

If one digit is changing at any point you will have a valid input. So checking the first input with all the following ones is enough.

如果一个数字在任何时候发生变化,您将获得有效输入。因此,使用以下所有内容检查第一个输入就足够了。

#3


0  

   int [][] theNumberArray = { {1,4,3},{6,2,3} };//inputs that will not give an error
   //int [][] theNumberArray = { {4,4,4},{6,2,3} };//inputs that will give an error 
   int lastEnteredValue = 0;
   int count = 0;


   for ( int x = 0; x < theNumberArray.length; x++ )
   {
        for ( int k = 0; k < theNumberArray[x].length; k++ )
        {

            if (k > 0 && theNumberArray[x][k] == lastEnteredValue) 
            {
                lastEnteredValue = theNumberArray[x][k];
                count += 1;

                if (count == theNumberArray[x].length - 1)
                {
                    System.out.println(
                    "Error : due to consecutively " + 
                    "inputting the same value!");
                    System.exit(-1);
                }
            } 
            else {
                lastEnteredValue = theNumberArray[x][k];
            }
        }
   }

   for (int x = 0; x < theNumberArray.length; x++)
   {
        for (int k = 0; k < theNumberArray[x].length; k++)
        {
            System.out.print(theNumberArray[x][k] + " ");
        }
            System.out.println();
   }

#1


1  

    Scanner input = new Scanner(System.in);
    int arr[] = new int[9];
    arr[0] = input.nextInt();
    boolean error = true;
    for (int i=1; i<9 ; i++)
    {
        arr[i] = input.nextInt();
        if (arr[i]!=arr[0])
            error = false;
    }

    if (error)
        System.out.println("Invalid");
    else
        System.out.println("valid");

#2


1  

Put the first input into a variable. If one input is not equal to the variable, you have a valid input. Otherwise you can reject it.

将第一个输入放入变量中。如果一个输入不等于变量,则输入有效。否则你可以拒绝它。

Consider the following input, which is invalid

请考虑以下输入,该输入无效

11111

If one digit is changing at any point you will have a valid input. So checking the first input with all the following ones is enough.

如果一个数字在任何时候发生变化,您将获得有效输入。因此,使用以下所有内容检查第一个输入就足够了。

#3


0  

   int [][] theNumberArray = { {1,4,3},{6,2,3} };//inputs that will not give an error
   //int [][] theNumberArray = { {4,4,4},{6,2,3} };//inputs that will give an error 
   int lastEnteredValue = 0;
   int count = 0;


   for ( int x = 0; x < theNumberArray.length; x++ )
   {
        for ( int k = 0; k < theNumberArray[x].length; k++ )
        {

            if (k > 0 && theNumberArray[x][k] == lastEnteredValue) 
            {
                lastEnteredValue = theNumberArray[x][k];
                count += 1;

                if (count == theNumberArray[x].length - 1)
                {
                    System.out.println(
                    "Error : due to consecutively " + 
                    "inputting the same value!");
                    System.exit(-1);
                }
            } 
            else {
                lastEnteredValue = theNumberArray[x][k];
            }
        }
   }

   for (int x = 0; x < theNumberArray.length; x++)
   {
        for (int k = 0; k < theNumberArray[x].length; k++)
        {
            System.out.print(theNumberArray[x][k] + " ");
        }
            System.out.println();
   }