如何在公共方法中使用私有方法来检查有效性?

发布于 2024-12-13 09:35:48 字数 1173 浏览 0 评论 0原文

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情仇皆在手 2024-12-20 09:35:48
public void setRating(int aRating)
{
   if (isValidRating(aRating))
       this.rating = aRating;
}

您的作业可能不需要这样做,但在现实世界中,您可能还需要考虑在验证失败时抛出 IllegalArgumentException。

例子:

public void setRating(int aRating)
{
    if (isValidRating(aRating))
        this.rating = aRating;
    else 
        throw new IllegalArgumentException("Invalid rating.");
}
public void setRating(int aRating)
{
   if (isValidRating(aRating))
       this.rating = aRating;
}

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:

public void setRating(int aRating)
{
    if (isValidRating(aRating))
        this.rating = aRating;
    else 
        throw new IllegalArgumentException("Invalid rating.");
}
陪你到最终 2024-12-20 09:35:48

您还必须将 aRating 参数传递给 isValid 方法。与其跳过错误值的赋值,通常最好抛出异常,如下所示:

public void setRating(int rating) {
  checkRating(rating);
  this.rating = rating;
}

private void checkRating(int rating) {
  if (!(1 <= rating && rating <= 10)) {
    throw new IllegalArgumentException("Invalid rating: " + rating);
  }
}

这种类型的参数检查由 java.util.ArrayList 等流行类使用,例如get(int) 方法。

You have to pass the aRating argument to the isValid method as well. And instead of just skipping the assignment for wrong values, it is often better to throw an exception, like this:

public void setRating(int rating) {
  checkRating(rating);
  this.rating = rating;
}

private void checkRating(int rating) {
  if (!(1 <= rating && rating <= 10)) {
    throw new IllegalArgumentException("Invalid rating: " + rating);
  }
}

This style of argument checking is used by popular classes like java.util.ArrayList, for example in the get(int) method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文