If statement 我的';如果';在Java中,语句被完全跳过。我怎样才能让他们得到认可?问题是什么?
导入java.util.Scanner 公共课3级26{If statement 我的';如果';在Java中,语句被完全跳过。我怎样才能让他们得到认可?问题是什么?,if-statement,If Statement,导入java.util.Scanner 公共课3级26{ public static void type() { System.out.println("Student or Teacher?"); Scanner input = new Scanner(System.in); String userInput = input.next(); if (userInput == "Student") { student("Jono", 12);
public static void type() {
System.out.println("Student or Teacher?");
Scanner input = new Scanner(System.in);
String userInput = input.next();
if (userInput == "Student") {
student("Jono", 12);
}
if (userInput == "Teacher") {
teacher("Mr. Fomenko", "Technology");
}
else {
System.out.println("DOES NOT COMPUTE");
System.out.println("TRY AGAIN");
type();
}
}
public static void student(String name, int grade) {
System.out.println(name + " is a student at Waterford High School. This person is in grade " + grade + ".");
}
public static void teacher(String name, String subject) {
System.out.println(name + " is a teacher at Waterford High School. This person teaches " + subject + "class.");
}
public static void main(String[] args) {
type();
}
}
这是我的代码。我这样做是为了课堂上的一个项目,出于某种原因,IFs只是被跳过了。无论您输入什么,即使“学生”或“老师”应该是正确的,它也会打印其他内容。有什么想法吗?在比较字符串时,使用
String.equals
而不是=
。因此,改变这一行:
if (userInput == "Student") {
到
可能的重复可能需要考虑将输入更改为上限或下限…如果
if (userInput.equals("Student")) {