我收到了一份學生 ID 號碼串列,供我在注冊程序中使用。然而,她發現在某些情況下,某些 ID 號碼是重復的。為避免重復,她打算首先對所有 ID 號進行編碼。您必須通過開發一個程式來幫助我解決我的困難,該程式允許我輸入身份證號碼而不必擔心重復。我的程式應該能夠在最后顯示 ID 號串列
匯入 java.util.Scanner;
公共類 ID_NUm {
public static void main(String[] args) {
int size;
Scanner sc = new Scanner(System.in);
System.out.print("How many students? ");
size = sc.nextInt();
int InpNum[] = new int[size];
System.out.print("\n");
for (int i = 0; i < size; i ) {
System.out.print("Enter ID number: ");
InpNum[i] = sc.nextInt();//this is the part where you should identify if the number is duplicated or not, and I don't know how to code that
}
System.out.print("\nList of ID number entered:\n\n");
for (int i = 0; i < size; i ) {
System.out.print("Enter ID number:" InpNum[i] " \n");
}
}
}
uj5u.com熱心網友回復:
您正在尋找使用HashSetJava 內置的 . 這是一種資料結構,您可以將元素插入其中,還可以檢查某個元素是否在恒定時間記憶體在。因此,當你讀入一個元素時,首先檢查集合是否包含該元素;如果是,請跳過它,否則,將其放入您的 ID 串列中,并將其插入您的 HashSet。您可以在此處查看檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/435832.html
