我正在与bluej和面向对象的指南书一起学习Java,很容易,我被困在练习的这个特定部分:“ setRefNumber mutator,以便仅当参数是至少三个字符串时才设置refNumber字段。字符。如果少于三个,则打印错误消息,并使该字段保持不变。”
我要尝试做的是:
public void setRefNumber (String ref)
{
refnumber = ref;
if (len < 3) {
System.out.println("reference number must have at least 3 characters");
}
else {
refnumber = ref;
}
最佳答案
您实际上需要使用函数length(),它是字符串库的一部分。见下文。应该解决您的问题
public void setRefNumber (String ref)
{
refnumber = ref;
if (ref.length() < 3) {
System.out.println("reference number must have at least 3 characters");
}
else {
refnumber = ref;
}
关于java - 如何创建比较字符串长度的条件语句?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32146444/