如何检查输入的数组值是否相同?

时间:2018-10-23 03:03:52

标签: java

我的程序应该确保用户输入的每个值都在10到100之间。然后将值存储在数组中。那部分工作正常。另一个条件是用户输入的值必须与所有其他数组不同。即... array [0] = 20,所以所有其他数组都不再等于20。我一直在尝试解决这个问题,但是我不确定该去哪里。我在while(userInput <10 || userInput> 100)之后尝试设置语句,以检查是否有重复,并且该方法有效。问题是用户可以输入小于10且大于100的值。将不胜感激任何帮助!

public static void main(String[] args) {
    //Creating scanner object
    Scanner input = new Scanner(System.in);
    int[] array = new int[5];
    int counter = 0;

    while(counter < 5)
    {
        for(int x = 0; x < array.length; x++)
        {
            System.out.print("Enter number between 10 & 100: ");
            int userInput = input.nextInt();

            while(userInput < 10 || userInput > 100)
            {                
                System.out.print("Please enter number between 10 & 100: ");
                userInput = input.nextInt();
            }

            array[x] = userInput;
            System.out.println(array[x]);
            counter++;

        }

    }

    System.out.println();
    System.out.println("The value of Array[0]: " + array[0]);
    System.out.println("The value of Array[1]: " + array[1]);
    System.out.println("The value of Array[2]: " + array[2]);
    System.out.println("The value of Array[3]: " + array[3]);
    System.out.println("The value of Array[4]: " + array[4]);        

}

}

4 个答案:

答案 0 :(得分:0)

您应该摆脱for和second while循环,并检查输入的值是否在所需范围内。

如果是,则验证是否重复,将其存储在数组中并增加计数器。如果不是,则会显示错误的输入消息。

无论哪种方式,它都会继续请求有效输入,直到计数器达到5。

希望对您有帮助!

答案 1 :(得分:0)

我稍微改变了你的逻辑,看看你是否能理解 (有更好的方法可以做到这一点,但我认为这更容易理解)

public static void main(String[] args) {
        //Creating scanner object
        Scanner input = new Scanner(System.in);
        int[] array = new int[5];
        int counter = 0;

        while(counter < 5)
        {
            System.out.print("Enter number between 10 & 100: ");
            int userInput = input.nextInt();

            if(userInput < 10 || userInput > 100)
            {                
                System.out.print("Please enter number between 10 & 100.\n");
            }
            else {
                //This is a valid input, now we have to check whether it is a duplicate
                boolean isItDuplicate = false;
                for(int i = 0; i < counter; i++)
                {
                    if(userInput == array[i])
                    {
                        isItDuplicate = true;
                    }
                }
                if(isItDuplicate == true)
                {
                    System.out.print("Please enter a number that is not a duplicate.\n");
                }
                else 
                {
                    array[counter] = userInput;
                    System.out.println(array[counter]);
                    counter++;
                }
            }
        }

        System.out.println();
        System.out.println("The value of Array[0]: " + array[0]);
        System.out.println("The value of Array[1]: " + array[1]);
        System.out.println("The value of Array[2]: " + array[2]);
        System.out.println("The value of Array[3]: " + array[3]);
        System.out.println("The value of Array[4]: " + array[4]);        

}

答案 2 :(得分:0)

  1. 当var counter为您做同样的事情时,请勿使用变量x
  2. 当需要在每次迭代中一起检查两个循环的极限条件时,不要使用嵌套循环。尽可能将这些循环合并为一个循环。

答案 3 :(得分:0)

首先,摆脱嵌套循环。他们是多余的。让我们看看您的问题的规范。您的输入需要满足3个要求:

  • 大于或等于10
  • 小于或等于100
  • 成为数组内的唯一元素

前两个条件很容易检查。对于最终条件,您需要搜索数组内的值,并查看输入是否匹配。如果是这样,则输入无效。一种简单的解决方法是检查数组的每个成员,并在发现重复项时停止。有更好,更有效的方法来执行此操作。不要害怕搜索互联网来学习搜索算法。以下是解决您的问题的简单方法。它远非理想或有效,但对于初学者来说很容易理解。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] array = new int[5];
        int counter = 0;
        int userInput;

        while(counter < 5) {
            System.out.print("Enter a number between 10 & 100: ");
            userInput = in.nextInt();
            if( (userInput >= 10 && userInput <= 100) && !searchDuplicates(array, userInput) ) {
                array[counter] = userInput;
                counter++;
            } else {
                System.out.println("Invalid value entered");
            }
       }
       for(int i = 0; i < array.length; i++)
           System.out.println("Value " + (i + 1)  + ": " + array[i]);
    }

    private static boolean searchDuplicates(int[] array, int input) {
        for(int i = 0; i < array.length; i++)
            if(array[i] == input)
                return true;
        return false;
    }
}