简体   繁体   English

如何使用System.out.println打印报告错误的消息,该消息显示无效参数但不存储该参数?

[英]How to print a message reporting an error with System.out.println that shows the invalid parameter but doesn't store it?

I need to print out the invalid parameter in the System.out.println message without storing it. 我需要在System.out.println消息中打印出无效参数而不存储它。

If the parameter is not between 1500 and 2000, I need to print a message saying "1400 is too early" or "2500 is too late". 如果参数不在1500到2000之间,我需要打印一条消息,说“ 1400太早”或“ 2500太晚”。

I wrote this but unfortunately it only prints the default parameter for the integer (0): 我写了这个,但不幸的是,它只输出整数(0)的默认参数:

    /**
     /* @param newModelYear is the model year of the car; invaild if the year is not in range between 1500 and 2000
     /*/
    public void setModelYear(int newModelYear){
        if ((modelYear <1500) && (modelYear >2000)) {
         modelYear=newModelYear;
        }
        else {
            System.out.println(getModelYear + "sorry it is too early!");
        }
     }

To sum it up, it should be: 总结起来,应该是:

public void setModelYear(int newModelYear){
    if ( newModelYear < 1500 ) {
        System.out.println(newModelYear + ": sorry it is too early!");
    } else if ( newModelYear > 2000)) {
        System.out.println(newModelYear + ": sorry it is too late!");
    } else {
        modelYear=newModelYear;
    }
 }

Explanation: instead of 说明:代替

        System.out.println(getModelYear + "sorry it is too early!");

write

        System.out.println(newModelYear + "sorry it is too early!");

This uses the passed-in variable newModelYear . 这使用传入的变量newModelYear

And consider that you need >= 1500 instead of <1500 and <= 2000 instead of >2000 , as @JB Nizet said. 并考虑您需要>= 1500而不是<1500以及<= 2000而不是>2000 ,如@JB Nizet所说。

Thirdly, you might want to compare 第三,您可能想比较

    if ((newModelYear >= 1500) && (newModelYear <= 2000)) {

instead of 代替

    if ((modelYear <1500) && (modelYear >2000)) {

You have your signs wrong. 你的迹象错了。 Change 更改

if ((modelYear <1500) && (modelYear >2000)) {
 modelYear=newModelYear;
}

To

if ((modelYear > 1500) && (modelYear < 2000)) {
 modelYear=newModelYear;
}

Remember, > means "greater than". 请记住,>表示“大于”。 < means "less than" <表示“小于”

These signs are called "Boolean operators". 这些符号称为“布尔运算符”。 They take they left and right sides of the operator, return true or false. 他们将其放在运算符的左侧和右侧,返回true或false。 The < symbol takes the left number, and checks if the right number is bigger. <符号使用左边的数字,并检查右边的数字是否更大。 If so, it returns true. 如果是这样,则返回true。 If it isn't it returns false. 如果不是,则返回false。

1 < 2

Returns true, because the number 1, on the left is less than the number 2, on the right. 返回true,因为左侧的数字1小于右侧的数字2。

if statements work using only true or false values. if语句仅使用true或false值工作。 Everything you use inside it returns one of these values. 您在其中使用的所有内容都会返回这些值之一。 Boolean operators take the values you use and turn them into boolean values that the if statement can use. 布尔运算符将您使用的值转换为if语句可以使用的布尔值。

I think using a try catch block (maybe more than one) to send the error you catch here to your parent code. 我认为使用try catch块(可能不止一个)将您在此处捕获的错误发送到父代码。 See the below link for more details. 有关更多详细信息,请参见下面的链接。

Java: Proper way to throw and catch an exception to validate input Java:抛出和捕获异常以验证输入的正确方法

The below code will resolve your problem 下面的代码将解决您的问题

  public void setModelYear(int newModelYear){
       if(newModelYear < 1500){
          System.out.println("It is too early");
       }
        else if(newModelYear > 2500){
          System.out.println("It is too late");
       }
       else{
          modelYear=newModelYear;
       }
     }

There is an easier way to tackle this problem: 有一个更简单的方法可以解决此问题:

/**
 /* @param newModelYear is the model year of the car; invaild if the year is not in range between 1500 and 2000
 /*/
public boolean setModelYear(int newModelYear){
    if(newModelYear < 1500){
        // Too old
        System.out.printf("Error: %d is too old.%n", newModelYear);
        return false;
    }else if(newModelYear > 2000){
        // Too new
        System.out.printf("Error: %d is too new.%n", newModelYear);
        return false;
    }else{
        // Just right (In range)
        // Set the object's model year to the new value
        modelYear = newModelYear;
        return true;
    }
 }

Hopefully that will help you. 希望对您有所帮助。 There's not much to say, but if you have any questions about it, feel free to ask. 没什么可说的,但是如果您对此有任何疑问,请随时提问。

Edit : I added a return to the function. 编辑 :我添加了返回功能。 The code can be used without a return ( public void setModelYear(...) ), but I decided to change the function to return a boolean. 可以使用该代码而无需返回( public void setModelYear(...) ),但是我决定更改该函数以返回布尔值。 If the newModelYear is valid and modelYear is changed, the function will return true to inform the calling function that the update was successful and the input provided was valid. 如果newModelYear有效并且更改了modelYear ,则该函数将返回true,以通知调用函数更新成功并且提供的输入有效。 Else, if the newModelYear is invalid, the code will still output an error to the console just like before, but it will now return false as well. 否则,如果newModelYear无效,则代码仍将像以前一样向控制台输出错误,但是现在它也将返回false。 If the returned value is false, this informs the caller that the input was not valid and modelYear was not changed. 如果返回的值为false,这将通知调用方输入无效,并且modelYear未被更改。

This should solve your problem. 这应该可以解决您的问题。

You just have to check if modelYear is less than 1500 or greater than 2000 separately in order to print their respective errors, and then set the new value if the value is between 1500 and 2000. 您只需要分别检查modelYear是否小于1500或大于2000即可打印它们各自的错误,然后在值介于1500和2000之间时设置新值。

public void setModelYear(int newModelYear){
    if (modelYear < 1500) {
        System.out.println(newModelYear + "sorry it is too early!");
    } else if (modelYear > 2000) {
        System.out.println(newModelYear + "sorry it is too late!");
    } else {
        modelYear = newModelYear;
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM