Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在数组中拾取唯一数字时出错_Java_Eclipse_Netbeans - Fatal编程技术网

Java 在数组中拾取唯一数字时出错

Java 在数组中拾取唯一数字时出错,java,eclipse,netbeans,Java,Eclipse,Netbeans,这是我的Java类教科书中的一个问题,其中用户输入10个整数。程序应该读取所有整数,并且只显示唯一的数字(不重复)作为输出。我很难理解为什么我的输出没有拾取数组中的最后一个唯一值(5)。有人能对这个问题给出一些见解吗?任何帮助都将不胜感激。(由于我们正处于课程的早期阶段,并且理解该语言,因此我们的任务是使用嵌套循环完成此任务。) -输出为: 输入10个数字:1232163452 不同数字的数目是5 不同的数字是:1 2 3 6 4 -何时应该: 输入10个数字:1232163452 不同数字的数

这是我的Java类教科书中的一个问题,其中用户输入10个整数。程序应该读取所有整数,并且只显示唯一的数字(不重复)作为输出。我很难理解为什么我的输出没有拾取数组中的最后一个唯一值(5)。有人能对这个问题给出一些见解吗?任何帮助都将不胜感激。(由于我们正处于课程的早期阶段,并且理解该语言,因此我们的任务是使用嵌套循环完成此任务。)

-输出为: 输入10个数字:1232163452 不同数字的数目是5 不同的数字是:1 2 3 6 4

-何时应该: 输入10个数字:1232163452 不同数字的数目是6 不同的数字是:123645


public class ch7e5{

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);


        System.out.print("Enter 10 numbers: ");

        int[] numberArray = new int[10];
        //create array for all numbers

        int[] distinctArray = new int[10];
        //create array for distinct numbers

        int distinct = 0;

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


            numberArray[i] = input.nextInt();

        distinctArray[0] = numberArray[0];
        //first value will be distinct

        for (int i = 1; i < numberArray.length; i++) {
        //loop to go through remaining values in numberArray

            boolean exists = false;
            //create boolean

            for (int j = 0; j < numberArray.length; j++) {
            //loop to check if value exists already in distinctArray

                if (numberArray[i] == distinctArray[j]) {

                    exists = true;


                    break;
                    //break out of inner loop

                }
            }

            if (exists == false) {
            //if value is unique then add it to the distinct array


                distinct++;
                distinctArray[distinct] = distinctArray[i];



            }
        }
        //}

        System.out.println("The number of distinct numbers is " + distinct);

        System.out.print("The distinct numbers are: ");

        for (int k = 0; k < distinct; k++)

            System.out.print(distinctArray[k] + " ");

    }

}```




公共类ch7e5{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
系统输出打印(“输入10个数字:”);
int[]numberArray=新int[10];
//为所有数字创建数组
int[]distinctArray=新int[10];
//为不同的数字创建数组
int=0;
对于(int i=0;i<10;i++)
numberraray[i]=input.nextInt();
distinctArray[0]=numberArray[0];
//第一个值将是不同的
for(int i=1;i
有3个不同的数字,1、2和3不是任何一个,因为它们出现了两次。输出应为6 4 5。我不知道你是怎么得到5个不同的数字的,也许你输入错了?我会尝试一开始不使用扫描仪,并尝试手动将数字放入数组中。此外,如果数字不同,我将创建一个长度为10的布尔数组,从all true开始记录。如果一个数字出现两次,数组中相应的布尔值将为false。一旦我写了它,我会用代码更新它

编辑:显然,拥有副本并不会将其从独特列表中删除。如果是这种情况,请详细说明标题

这是我的密码:

public class Main {
  public static void main(String[] args) {
    int inputs = 3;
    int[] numberArray = new int[inputs];
    int distinct = 0;
    boolean[] mirror = new boolean[inputs];

    //just setting up arrays
    for(int i = 0; i < numberArray.length; i++) {
      //numberArray[i] = i;
      mirror[i] = true;
    }
    numberArray[0] = 1;

    //finds out what numbers are not distinct
    for (int x = 0; x < numberArray.length; x++) {
      for (int y = 0; y < numberArray.length; y++) {
        System.out.println("x is " + x);
        System.out.println("y is " + y);
        if(numberArray[x] == numberArray[y] && x != y) {
          System.out.println(numberArray[x] + " is not distinct");
          mirror[x] = false;//if current position in array matches any other position, number is not distinct
        }
      }
    }

    //calculates how many are distinct
    for(int j = 0; j < inputs; j++) {
      if(mirror[j]) {distinct++;}
    }

    //outputs text
    System.out.println("The number of distinct numbers is " + distinct);
    System.out.print("The distinct numbers are: ");
    for(int k = 0; k < inputs; k++){
      if(mirror[k]) {System.out.print(numberArray[k] + " ");}
    }
  }
}
公共类主{
公共静态void main(字符串[]args){
int输入=3;
int[]numberArray=新的int[输入];
int=0;
布尔[]镜像=新布尔[输入];
//只是设置阵列
for(int i=0;i
错误就在这里

distinct++;
distinctArray[distinct] = distinctArray[i];
1,在向
distinctaray
添加一个数字后,应该递增
distinctArray
,2,应该执行
distinctArray[distinct]=numberArray[i]
。现在,您只是将可能是
0
的内容放入
distinctArray[distinct]

一个较短的方法是这样做

int[] distinctArray = Arrays.stream(numberArray).distinct().toArray();
System.out.println("The number of distinct numbers is " + distinctArray.length);
System.out.print("The distinct numbers are: ");
for (int d : distinctArray) System.out.print(d + " ");
输出:

Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2
The number of distinct numbers is 6
The distinct numbers are: 1 2 3 6 4 5

1,1-两次但是distinct@TraianGEICU因为1出现了两次,所以它是不明确的,但是?
它应该出现的时间:输入10个数字:1 2 3 2 1 6 3 4 5 2明确的数字是6,明确的数字是:1 2 3 6 4 5
。好吧,现在结果出来了@但是你说“读取所有整数,只显示唯一的数字(不重复)作为输出。”1,1-两次,但不同,我说的是
distinct++;distinctArray[distinct]=distinctArray[i]
尝试交换,先添加,再增加计数器