私のJavaクラスでは、配列について学習していましたが、この質問が出てきました。私はそれを解決しようとしましたが、要件を満たせないようです。ユーザー入力を読み込んで、5つの要素(他の要件の1つ)に制限することができます。また、値は10から100の間でなければなりません。これも実行しました。しかし、重複した値を「印刷しない」ようには見えません。配列は重複する値を受け入れます。印刷するだけでなく、取り出す必要はありません。これまでの私のコードは次のとおりです。
import java.util.Arrays;
import java.util.Scanner;
public class ArrayTest {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int size = 5;
int InpNum[] = new int[size];
for (int i = 0; i < InpNum.length; i++){
while (InpNum[i] <= i){
System.out.println("Please type a number between 10 and 100: ");
InpNum[i] = in.nextInt();
while (InpNum[i] < 10 || InpNum[i] > 100){
System.out.println("Error: Please type an integer between 10 and 100: ");
InpNum[i] = in.nextInt();
}
Arrays.sort(InpNum);
System.out.println(Arrays.toString(InpNum));
}
while (Search(InpNum, i) == true){
System.out.println("ERROR: Please enter a number that is not a duplicate of the other numbers you have entered");
InpNum[i] = in.nextInt();
}
}
}
// I can't seem to implement the method below in a useful manner.
public static boolean Search(int InpNum[], int searchedNum) {
for(int i : InpNum) {
if (i == searchedNum) {
return true;
}
}
return false;
}
}