为什么我只得到 else 声明?
Why am i only getting towards else statement?
请检查以下程序并指出错误。无论输入是什么,我都会得到 "not ok" 结果。
package timePass;
public class TimePass{
private String password;
public String getPassword() {
return password;
}
public void setPassword() {
password="no";
}
}
package timePass2;
import java.util.Scanner;
import timePass.TimePass;
public class TimePass2{
public static void main(String[] args) {
TimePass obj= new TimePass();
Scanner sc= new Scanner(System.in);
String p= sc.nextLine();
TimePass tp= new TimePass();
if(p.equals(tp.getPassword())) {System.out.println("ok");}
else {System.out.println("not ok");}
}
}
tp
的密码尚未设置。您必须显式调用 tp.setPassword()
,否则 tp.password
是 null
请检查以下程序并指出错误。无论输入是什么,我都会得到 "not ok" 结果。
package timePass;
public class TimePass{
private String password;
public String getPassword() {
return password;
}
public void setPassword() {
password="no";
}
}
package timePass2;
import java.util.Scanner;
import timePass.TimePass;
public class TimePass2{
public static void main(String[] args) {
TimePass obj= new TimePass();
Scanner sc= new Scanner(System.in);
String p= sc.nextLine();
TimePass tp= new TimePass();
if(p.equals(tp.getPassword())) {System.out.println("ok");}
else {System.out.println("not ok");}
}
}
tp
的密码尚未设置。您必须显式调用 tp.setPassword()
,否则 tp.password
是 null