我試着運行該程序,它運行良好。如果假設我輸入了day
作爲輸入。然後它進入if
一次和其他內部(關鍵字-1的長度)時間。請檢查您是否輸入了與關鍵字數組中給定的值相同的值。
package myApp;
import java.util.Scanner;
public class Client{
public static void main(String[] args) {
String[] keywords = { "day", "Cs 377", "What book", "office", "hour",
"e-mail", "name", "major", "student e-mail", "group id",
"lectures", "lecture room", "lecture time",
"number of lectures", "current lecture",
"topics of current lecture", "number of test",
"date of a test", "number of assignments", "sure",
"current assignment", "due day" };
Scanner scanner = new Scanner(System.in);
String input = null;
/* -end init- */
System.out.println("Welcome ");
System.out.println("What's on your mind?");
do {
System.out.print("> ");
input = scanner.nextLine().toLowerCase();
for (String keyword : keywords) {
if (input.contains(keyword)) {
System.out.println("KEYWORD:" + keyword);
break;
}
else {
System.out.println("KEYWORD not found");
}
}
break;
} while (input.equalsIgnoreCase("bye"));
System.out.println("Have a good day!");
}
}
我更換了一些東西里面,如果和其他與sysout。 我在輸入一天輸入並得到以下結果。
Welcome
What's on your mind?
> day
KEYWORD:day
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
KEYWORD not found
Have a good day!
這個輸出很好理解。如果您覺得困難,請在下面評論您所面臨的問題。
你的輸入是什麼?什麼是關鍵字?它不是「被忽略」,它可能總是「錯誤的」 –
@OriLentz第二個執行。這意味着它並不總是假的。 – CKing
可能有兩個原因,要麼控制沒有進入for循環,要麼if中提到的條件是false,需要更多的代碼來了解 –