4 回答

TA貢獻1862條經(jīng)驗 獲得超7個贊
您不能將 an 分配int
給整個數(shù)組
studID[] = scan.nextInt();
您需要做的是將其分配給數(shù)組的一個元素,例如
studID[0] = scan.nextInt();
或者
studID[i] = scan.nextInt();
i
索引在哪里
但
因為你沒有循環(huán)或使用多個值,為什么你甚至有數(shù)組?

TA貢獻1811條經(jīng)驗 獲得超4個贊
給定代碼,您不需要數(shù)組來存儲字符串或整數(shù):
int[] studID = new int[100];
String[] Lname = new String[100];
String[] Fname = new String[100];
String[] studProgram = new String[100];
int[] studYear = new int[100];
只需像這樣聲明它們:
int studID;
String Lname;
String Fname;
String studProgram;
int studYear;
在這個開關(guān)盒中:
switch(choice){
case 1:
System.out.print("Enter ID: ");
studID = scan.nextInt();
System.out.print("Enter Last name: ");
Lname = scan.next();
System.out.print("Enter First name: ");
Fname = scan.next();
System.out.print("Enter Course: ");
studProgram = scan.next();
System.out.print("Enter Year: ");
studYear = scan.nextInt();
}
nextInt() 方法返回一個整數(shù),而不是一個數(shù)組,next() 返回一個字符串,而不是字符串?dāng)?shù)組。如果您需要任何幫助,我們非常樂意為您提供幫助。

TA貢獻1712條經(jīng)驗 獲得超3個贊
你在這里做錯了幾件事。首先,它應(yīng)該是這樣的。; 預(yù)期在 java 中結(jié)束語句而不是 .(點)。
int studView = scan.nextInt();
現(xiàn)在,您插入數(shù)組元素的邏輯不正確。您必須知道,數(shù)組是存儲在特定索引處的許多元素的集合。因此,您需要將 scan.nextInt() 中的 elem 存儲在特定索引處。為了那個原因,
for(int i=0;i<someLength;i++){
int choice = scan.nextInt();
switch(choice){
case 1:
System.out.print("Enter ID: ");
studID[i] = scan.nextInt(); // This i is important here to store at some particular index
......................
....................
}
}

TA貢獻1982條經(jīng)驗 獲得超2個贊
除了其他答案之外,如果您計劃動態(tài)地向其添加整數(shù),那么使用ArrayList可能會更好。它不需要預(yù)先確定的大小,并允許您將一個整數(shù)添加到數(shù)組的末尾。希望這可以幫助。
添加回答
舉報