配列内の個別の数値を出力する小さなプログラムを作成しようとしています。たとえば、ユーザーが1,1,3,5,7,4,3と入力した場合、プログラムは1,3,5,7,4のみを出力します。
関数のelseif行でエラーが発生します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;
}
}