所以,如果我理解正確的話,你需要做的是瞭解以下信息:
if (containsName(studentNamesArray, studentName) == false) {
addStudent(studentNamesArray, studentName);
}
除此之外,這可能與Java Set可以輕鬆解決這樣的:
studentSet.add(studentName); // automatically ignores duplicates
所有你需要做的第一個代碼部分是編寫函數containsName
和addStudent
boolean containsName(String[] studentNamesArray, String studentName) {
for (int i = 0; i < studentNamesArray.length; ++i) { // specialized while loop over all array elements
if (studentName.equals(studentNamesArray[i])) { // use equals for String comparison! This will as well not match on "null"
return true; // we found a match
}
}
return false; // no match was found
}
而且
void addStudent(String[] studentNamesArray, String studentName) {
int i = 0;
while ((studentNamesArray[i] != null) && // this student entry is already filled by someone else
(i < studentNamesArray.length)) { // we are still within the array bounds
++i;
}
if (i < studentNamesArray.length) { // is i still within the array bounds?
studentNamesArray[i] = studentName; // add the student
} else {
// we have too many students. What to do now?
}
}
有這個代碼不檢查幾件事情:
- 如果有更多的學生比數組的大小?
- 如果studentName是
null
?
這種寫代碼的方法叫做Top-down approach,如果你知道一般的程序邏輯,但不知道細節,那麼編碼就容易多了。
編輯:的完整代碼:
import java.util.Scanner;
public class Test {
static boolean containsName(String[] studentNamesArray, String studentName) {
for (int i = 0; i < studentNamesArray.length; ++i) { // specialized while loop over all array elements
if (studentName.equals(studentNamesArray[i])) { // use equals for String comparison! This will as well not match on "null"
return true; // we found a match
}
}
return false; // no match was found
}
static void addStudent(String[] studentNamesArray, String studentName) {
int i = 0;
while ((studentNamesArray[i] != null) && // this student entry is already filled by someone else
(i < studentNamesArray.length)) { // we are still within the array bounds
++i;
}
if (i < studentNamesArray.length) { // is i still within the array bounds?
studentNamesArray[i] = studentName; // add the student
} else {
// we have too many students. What to do now?
}
}
public static void main(String[] args) {
final String[] studentNamesArray = new String[10];
String studentName;
final Scanner scanner = new Scanner(System.in);
do {
System.out.println("Please insert your name:");
studentName = scanner.nextLine();
if (studentName.length() > 0) {
if (containsName(studentNamesArray, studentName) == false) {
addStudent(studentNamesArray, studentName);
System.out.println(studentName + " added.");
} else {
System.out.println(studentName + " was already in the list.");
}
}
} while (studentName.length() > 0);
for (int i = 0; i < studentNamesArray.length; ++i) {
System.out.println("Student #" + i + ": " + studentNamesArray[i]);
}
}
}
使用樣本輸入,我得到如下:
Please insert your name:
Two
Two added.
Please insert your name:
Test
Test added.
Please insert your name:
Test
Test was already in the list.
Please insert your name:
test
test added.
Please insert your name:
test
test was already in the list.
Please insert your name:
Student #0: Two
Student #1: Test
Student #2: test
Student #3: null
Student #4: null
Student #5: null
Student #6: null
Student #7: null
Student #8: null
Student #9: null
您只給出部分代碼。此代碼之前的實際輸入是否讀取?如果是這樣的話,它在根本上被打破了,因爲在循環期間沒有新的數據被讀取,使得它一遍又一遍地使用相同的字符串。 – Smallhacker
輸入讀數確實在顯示的代碼之前。將輸入的值傳遞給一個單獨的函數將它們插入到數組中會更好嗎? – Colin747