//詢問用戶他想要多少學生和檔次,進入無法弄清楚如何將用戶inputed名添加到陣列中的Java
public void setup() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter number of students: ");
numStudents = keyboard.nextInt();
}
public void setName(String someName) {
for(int i = 0; i < numStudents; i++) {
System.out.print("Enter name of student # " + (i+1) + ": ");
Scanner keyboard = new Scanner(System.in);
String nameRough = keyboard.nextLine();
name = nameRough.trim();
// i have to ask the user to enter the name int he for Last, first and then rearrange it so this is what thats doing
String firstName = name.substring(name.indexOf(",")+1);
String lastName = name.substring(0, name.indexOf(","));
name = firstName + " " + lastName;
// name = nameList;
System.out.println("");
// and then here is where i can't figure out how to add name to the army at i
String[] namesList = new String[numStudents];
nameList[i] = name;
}
}
我想就能夠在用戶輸入要添加的名字但是在格式正確之後。我只知道如何把它們在他們眼前時添加到字符串獲取用戶輸入的
你是什麼意思,但他們格式正確後? – aribeiro
您可以顯示您的輸出/運行結果:運行您目前爲止所做的 - 將幫助其他人爲您提供幫助。 – hamish
你可能想看看你的名字爲'split(「,」)'的方法 –