在一个阵列打印不同的整数 [英] Printing distinct integers in an array

查看:188
本文介绍了在一个阵列打印不同的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个小程序,在一个阵列打印出不同的数字。例如,如果用户输入1,1,3,5,7,4,3程序将只打印出1,3,5,7,4。

我在功能checkDuplicate的否则,如果行收到错误。

下面是我的code迄今:

 进口javax.swing.JOptionPane中;     公共静态无效的主要(字串[] args){
    INT []数组=新INT [10];
    的for(int i = 0; I< array.length,我++){
      数组[我] =的Integer.parseInt(JOptionPane.showInputDialog(请输入
                                  +的整数:));
    }
    checkDuplicate(数组);
  }
  公共静态INT checkDuplicate(int数组[]){
  的for(int i = 0; I< array.length,我++){
    布尔发现= FALSE;
    对于(INT J = 0; J< I; J ++)
      如果(阵列[我] ==阵列[J]){
        发现= TRUE;
        打破;
      }
    如果(!找到)
      的System.out.println(数组[我]);
  }
  返回1;
 }
}


解决方案

首先,在否则,如果说法是不正确的,因为你不提供任何条件的,如果(如果你想要一个如果,你需要写如果(条件)... )。

二,你不能决定的内的内的循环,如果一个值应印:你的code ++工程,你写一个值数组[我]的每个值数组的方式[J] 的就是从数组不同[I]!

第三:内环只需从0到去外指数 I-1 :对于每一个元素,您只需要来决定,如果它是第一个发生(即如果相同的值发生在任何previous指数与否)。如果是,把它打印出来,如果​​不是,忽略它。

一个正确实施 CheckDuplicate的()是:

 公共静态无效checkDuplicate(int数组[]){
  的for(int i = 0; I< array.length,我++){
    布尔发现= FALSE;
    对于(INT J = 0; J< I; J ++)
      如果(阵列[我] ==阵列[J]){
        发现= TRUE;
        打破;
      }
    如果(!找到)
      的System.out.println(数组[我]);
  }
}

不过,当然,某种将是更大的阵列更有效...


编辑:当然的 mmyers 的(见注释)是正确的说法,既然 CheckDuplicate()不返回任何值,它应该有返回类型(而不是 INT 无效。我在上面code纠正了这个...

I'm trying to write a small program that prints out distinct numbers in an array. For example if a user enters 1,1,3,5,7,4,3 the program will only print out 1,3,5,7,4.

I'm getting an error on the else if line in the function checkDuplicate.

Here's my code so far:

    import javax.swing.JOptionPane;

     public static void main(String[] args) {
    int[] array = new int[10];
    for (int i=0; i<array.length;i++) {
      array[i] = Integer.parseInt(JOptionPane.showInputDialog("Please enter"
                                  + "an integer:"));
    }
    checkDuplicate (array);
  }
  public static int checkDuplicate(int array []) {
  for (int i = 0; i < array.length; i++) {
    boolean found = false;
    for (int j = 0; j < i; j++)
      if (array[i] == array[j]) {
        found = true;
        break;
      }
    if (!found)
      System.out.println(array[i]);
  }
  return 1;
 }
}

解决方案

First of all, the "else if" statement is incorrect, since you don't provide any condition to the if (if you want an if, you need to write "if (condition) ...").

Second, you cannot decide inside the inner loop, if a value should be printed: The way your code works you write a value array[i] for each value array[j] that is different from array[i]!

Third: the inner loop needs only to go from 0 to the outer index i-1: For each element, you need only to decide, if it is the first occurrence (i.e. if the same value occured at any previous index or not). If it is, print it out, if not, ignore it.

A proper implementation of CheckDuplicate() would be:

public static void checkDuplicate(int array []) {
  for (int i = 0; i < array.length; i++) {
    boolean found = false;
    for (int j = 0; j < i; j++)
      if (array[i] == array[j]) {
        found = true;
        break;
      }
    if (!found)
      System.out.println(array[i]);
  }
}

But of course, some kind of Set would be much more efficient for bigger arrays...


EDIT: Of course, mmyers (see comments) is right by saying, that since CheckDuplicate() doesn't return any value, it should have return type void (instead of int). I corrected this in the above code...

这篇关于在一个阵列打印不同的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆