import java.util.Scanner;
public class Dx {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
String day = myObj.nextLine();
if (day == "morning") {
System.out.println("Good Morning");
} else if (day == "afternoon") {
System.out.println("Good Afternoon");
} else if (day == "evening") {
System.out.println("Good evening!");
} else if (day == "night") {
System.out.println("Good night");
} else {
System.out.println("Good Day");
}
}
}
此处显示晚上或早晨时的输出显示Good Day。
最好使用.equals()方法比较Java中的字符串。
if (stringA.equals(stringB)) {
...
}