当我的输入为空时,我试图让我的代码返回一条语句。这是我当前的代码。
public boolean inputLastname(){
System.out.println("Enter New Employee Information");
System.out.print("Enter Employee Last Name: ");
lastName = sc.next();
if(lastName.trim().equals(" "))
return false;
return true;
}
我希望它显示“未输入员工信息”并返回 false。 我该怎么做呢?谢谢。
最佳答案
我建议这样:
public boolean inputLastname(){
System.out.println("Enter New Employee Information");
System.out.print("Enter Employee Last Name: ");
lastName = sc.nextLine();
Boolean returnBool = true;
if(lastName.isEmpty()){
returnBool = false;
System.out.println("No Employee Information Entered");
}
return returnBool;
}
我认为通过测试字符串是否为空更直接。由于只有一个 return 语句,这也使得它更干净。
关于java - 输入为空时如何返回语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36821205/