如何在setter中输入if语句的字符串长度 就像我希望用户在课程名称中但在setter中我想把if语句完全像4个字符长
if ( (??????) == 4 )
n= name ;
else
System.out.println("it should be 4 char long") ;
答案 0 :(得分:0)
我假设您使用的是Java
。
这是您可以在解决方案中实现的一种逻辑:
public void setCourseName(String name){
if(name == null || name.length() != 4){
//Do something which will handle the incorrect data
System.out.println("Incorrect value!");
}else{
System.out.println("Correct value!");
}
}