Java-如何检查输入的整数是否唯一

Java-如何检查输入的整数是否唯一,java,Java,基本上,我的代码从用户那里获取输入,并使用一种方法检查输入是否唯一。 如果不是唯一的,请要求用户输入另一个 我的想法是: import java.util.Scanner; public class testing { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] input = new int[10]; for

基本上,我的代码从用户那里获取输入,并使用一种方法检查输入是否唯一。 如果不是唯一的,请要求用户输入另一个

我的想法是:

import java.util.Scanner;
public class testing {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);


        int[] input = new int[10];

        for (int i = 0; i < 10; i++) {

            while (true) {
                int n = sc.nextInt();
                for (int j = 0; j < input.length; j++) {
                    input[j] = n;
                    checkunique(input);
                    System.out.println("Choose a different one");
                }
                break;
            }
        }

        public static boolean checkunique(int[] Array) {
            for (int i = 0; i < Array.length - 1; i++) {
                for (int j = 1 + 1; j < Array.length; j++) {
                    if (Array[i] == Array[j]) {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
import java.util.Scanner;
公共类测试{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
int[]输入=新的int[10];
对于(int i=0;i<10;i++){
while(true){
int n=sc.nextInt();
对于(int j=0;j

很抱歉,从这一点开始,我无法理解其余部分。

您发布的代码应该可以。如果用户数量非常多,则可能需要使用。

在将输入值添加到数组之前,可以检查输入值是否与数组中的另一个值相同。您可以通过让您的检查方法采用如下两个参数来实现:

public static boolean checkunique(int testValue, int[] array) {
    for (int i = 0; i < array.length; i++) {
        if (testValue == array[i]) {
            return false;
        }
    }
    return true;
}
注意:
由于初始数组由零组成,因此输入零将被视为重复。如果要解决此问题,只需将数组值初始化为所选的默认值。

您可以使用添加元素并检查其是否唯一。每次使用add函数在集合中添加元素时,如果再次添加相同的元素,则返回true。因此,您可以通知用户选择不同的选项。

以下是一种方法。 您可以维护一个集合,在其中存储用户输入,java中的集合只允许唯一的数据。因此,您所要做的就是检查用户输入的号码是否已经存在于您的集合中

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class SOTest {

    private static Set<Integer> uniqueInput = new HashSet<>();

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {

            while (true) {
                System.out.println("Choose a number:-");
                int n = sc.nextInt();

                if (!uniqueInput.contains(n)) {
                    uniqueInput.add(n);
                    if(uniqueInput.size()==10) break;
                } else {
                    System.out.println("Duplicate input");
                    continue;
                }
            }
        }
        for(Object i:uniqueInput.toArray()) {
            System.out.print(i+" ");
        }
    }
}
import java.util.HashSet;
导入java.util.Scanner;
导入java.util.Set;
公共类SOTest{
私有静态集uniqueInput=newhashset();
公共静态void main(字符串[]args){
try(Scanner sc=新扫描仪(System.in)){
while(true){
System.out.println(“选择一个数字:-”);
int n=sc.nextInt();
如果(!uniqueInput.contains(n)){
uniqueInput.add(n);
如果(uniqueInput.size()==10)中断;
}否则{
System.out.println(“重复输入”);
继续;
}
}
}
对于(对象i:uniqueInput.toArray()){
系统输出打印(i+“”);
}
}
}
说明: 在本例中,我们只是使用一个方法创建了另一个类,该方法可以为您执行检查。我们将把用户输入存储在ArrayList中,然后在主程序中将该ArrayList转换为字符串数组。当我们在主类中调用该方法时,我们可以将该数组传递给在另一个类中创建的方法,并且我们可以检查当前输入是否已经在传递给我们的方法的数组中。我们将在我们创建的方法中使用一个布尔值作为“标志”,如果该“标志”的计算结果为“true”,我们就知道用户输入已经在数组中。然后,我们在主程序中使用该标志作为条件测试,如果它为“true”,我们就知道用户输入已经被存储。如果用户输入已经在该阵列中,我们将要求用户提供更多输入,而不会将其存储在主存储阵列中。但是,如果用户输入是“唯一的”,我们将确认输入已存储,并将其放入主阵列中。这就是程序的要点,正如您所见,将其分解为类和方法在这里很有用

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class SOTest {

    private static Set<Integer> uniqueInput = new HashSet<>();

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {

            while (true) {
                System.out.println("Choose a number:-");
                int n = sc.nextInt();

                if (!uniqueInput.contains(n)) {
                    uniqueInput.add(n);
                    if(uniqueInput.size()==10) break;
                } else {
                    System.out.println("Duplicate input");
                    continue;
                }
            }
        }
        for(Object i:uniqueInput.toArray()) {
            System.out.print(i+" ");
        }
    }
}
import java.util.*;


class InputChecker {

   public static boolean checkInput(String[] a, String b) {
       boolean isIn = false;
       for(int x = 0; x < a.length - 1; x++) {
           if(x == 1) {
              if(a[1].equals(a[0]))
                   isIn = true;
        }

        if(a[a.length - 1].equals(a[x]))
            isIn = true;
       }
        return isIn;
}

}

public class MainChecker {

   public static void main(String[] args){
        Scanner receive = new Scanner(System.in);
        InputChecker check = new InputChecker();
        ArrayList<String> checkMe = new ArrayList<String>();
        ArrayList<String> validEntry = new ArrayList<String>();
        while(true) {
        System.out.println("Please enter some input, or end to quit: ");
        String userInput = receive.nextLine();
        if(! userInput.equals("end")) {
                checkMe.add(userInput);
                String[] k =  new String[checkMe.size()];
                k = checkMe.toArray(k);
                boolean bool = check.checkInput(k, userInput);
                if(bool) {
                    System.out.println("Please enter another input: ");
                }
                else {
                    System.out.println("Input stored!");
                    validEntry.add(userInput);
                }

        }


        }

    }

}
Please enter some input, or end to quit: 
Sim
Input stored!
Please enter some input, or end to quit: 
Yo
Input stored!
Please enter some input, or end to quit: 
Sim
Please enter another input: 
Please enter some input, or end to quit: 
Yo
Please enter another input: 
Please enter some input, or end to quit: