에이바*_*에이바 0 java arrays duplicates
我正在尝试编写一个小程序,在数组中打印出不同的数字.例如,如果用户输入1,1,3,5,7,4,3,程序将只打印1,3,5,7,4.
如果函数中的行,我在else上收到错误checkDuplicate.
到目前为止,这是我的代码:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17478 次 |
| 最近记录: |