如何在公共方法中使用私有方法来检查有效性?
private boolean isValid(int aRating)
{
return aRating >= 1 && aRating <= 10;
}
or
private boolean isValid(int aRating)
{
if (aRating >=1 && aRating <=100
return true;
else
return false
}
我现在需要编写一个方法 setRating(aRating),如果评级有效,则将评级设置为 aRating。所以我假设我需要在公共方法中使用上面的方法来检查它是否有效。如果它有效,我需要将 rating = 设置为 aRating。到目前为止,我的想法是这样的:
public void setRating(int aRating)
{
if (isValid() == true)
rating = aRating;
}
但我不能使用 isValid 作为 true 的 == 因为它是一种方法。我也尝试使用 isValid();为了尝试使用该方法,但由于顶部的 (int aRating) ,它不允许。如果我没有标识符,那么它根本不允许我使用 aRating...
现在
public void setRating(int aRating)
{
if (isValid(aRating))
rating = aRating;
}
我需要创建一个 setRating() 方法,允许用户从键盘输入某些内容,并且它必须是有效的,我不断收到错误,因为它说我不能重载 SetRating(int aRating) 两次,我明白。但是如果我尝试取出 int 部分,它是无效的,因为它需要一个参数。这就是我所拥有的
public void setRating()
{
Scanner keyboard = new Scanner(System.in);
if (isValid(aRating))
rating = keyboard.nextInt();
}
private boolean isValid(int aRating)
{
return aRating >= 1 && aRating <= 10;
}
or
private boolean isValid(int aRating)
{
if (aRating >=1 && aRating <=100
return true;
else
return false
}
I now need to Write a method setRating(aRating) that sets the rating to aRating IF it is valid. So i am assuming that i need to use the method above in a public method to check if it is valid. If it is valid i then need to set rating = to aRating. So far my idea has been this:
public void setRating(int aRating)
{
if (isValid() == true)
rating = aRating;
}
But i cannot use isValid as a == to true because it is a method. i also try using isValid(); in order to just try and use the method but it wont allow because of the (int aRating) at the top. If i do not have the identifier it then wont allow me to use aRating at all...
Now that it is
public void setRating(int aRating)
{
if (isValid(aRating))
rating = aRating;
}
I need to make a setRating() method that allows the user to input something from the keyboard and again it has to be valid, i keep getting an error because it says i cant overload SetRating(int aRating) Twice, which i understand. But if i try to take the int part out it is invalid because it needs a parameter. this is what i have
public void setRating()
{
Scanner keyboard = new Scanner(System.in);
if (isValid(aRating))
rating = keyboard.nextInt();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的作业可能不需要这样做,但在现实世界中,您可能还需要考虑在验证失败时抛出 IllegalArgumentException。
例子:
This might not be required for your homework, but in the real world you may also want to consider throwing an IllegalArgumentException if validation fails.
Example:
您还必须将
aRating
参数传递给isValid
方法。与其跳过错误值的赋值,通常最好抛出异常,如下所示:这种类型的参数检查由
java.util.ArrayList
等流行类使用,例如get(int)
方法。You have to pass the
aRating
argument to theisValid
method as well. And instead of just skipping the assignment for wrong values, it is often better to throw an exception, like this:This style of argument checking is used by popular classes like
java.util.ArrayList
, for example in theget(int)
method.