如何编写一个对某些参数返回 True 的私有方法?

发布于 2024-12-12 20:34:37 字数 805 浏览 0 评论 0原文

所以准确的写法是 “编写一个私有方法 isValid(aRating),如果给定的评级有效(介于 1-10 之间),则返回 true。”

private void isValid(aRating)
{

}

如果给定的评级有效,则上述方法中的内容是为了返回真值,“有效性”表示数字 1-10。

这就是我尝试的,教练希望它是“正确的方式”,这只是我尝试的一个例子,它是一个完全不同的程序。(如果令人困惑,请忽略)。

private void isValid(int hour,  int minute) 
{
    if (hour >= 0 && hour <=23) 
    {
        System.out.println("Hour is valid");
        hourIsValid = true;
    } 

    else 
    {
        System.out.println("Hour is not valid");
        hourIsValid = false;
        System.exit(0);
    }
}

这是正确的吗

    private boolean isValid(int aRating)
{                     
     if (aRating >= 1 && aRating <= 10)
         return true;
     else
         return false;
}

So the exact wording is,
"Write a private method isValid(aRating) that returns true if the given rating is valid, which is between 1-10."

private void isValid(aRating)
{

}

What Goes in the Method above in order to return a true value if given rating is valid, Validity meaning a number 1-10.

This is what i attempted, the instructor wants it the "proper way" This is just an example of what i attempted at, It is a different program completely.(Ignore if confusing).

private void isValid(int hour,  int minute) 
{
    if (hour >= 0 && hour <=23) 
    {
        System.out.println("Hour is valid");
        hourIsValid = true;
    } 

    else 
    {
        System.out.println("Hour is not valid");
        hourIsValid = false;
        System.exit(0);
    }
}

Would this be correct

    private boolean isValid(int aRating)
{                     
     if (aRating >= 1 && aRating <= 10)
         return true;
     else
         return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

偏爱你一生 2024-12-19 20:34:37

该问题要求函数在某些条件下返回 true,因此签名不能

private void isValid(int aRating) {}

需要返回类型。现在,true的类型是布尔值,所以将其设置为

private boolean isValid(int aRating) {
    return /* validity test here */;
}

The problem demands a function that returns true under some conditions, so the signature cannot be

private void isValid(int aRating) {}

it needs a return type. Now, true's type is boolean, so make it

private boolean isValid(int aRating) {
    return /* validity test here */;
}
睡美人的小仙女 2024-12-19 20:34:37

调用私有方法

从公共方法调用私有方法与调用任何其他方法完全相同:

public void doStuff() {
    System.out.println("Stuff done.");
}

private void doOtherStuff() {
    System.out.println("Other stuff done.");
}

public void showHowItWorks() {
    doStuff();
    doOtherStuff();

    // or if you prefer this style:
    this.doStuff();
    this.doOtherStuff();
}

重要的区别在于,您只能从定义私有方法的类内部调用私有方法:

class PublicExample {
    public static void doStuff() {
        System.out.println("Stuff done.");
    }
}

class PrivateExample {
    private static void doStuff() {
        System.out.println("Stuff done.");
    }
}

class Example {
    public static void Main(String[] args) {
        PublicExample.doStuff(); // Works
        PrivateExample.doStuff(); // Doesn't work (because it's private and defined in a different class)
    }
}

返回值

private void isValid(int hour,  int minute) {
    if (hour >= 0 && hour <=23) {
        System.out.println("Hour is valid");
        hourIsValid = true;
    } else {
        System.out.println("Hour is not valid");
        hourIsValid = false;
        System.exit(0);
    }
}

这种方法的问题在于:如果您输入错误的小时或分钟,您的程序会立即终止。如果我想循环直到获得良好的输入怎么办?

我认为主要问题是您不知道如何 返回值来自方法。下面是一个始终返回 true 的函数示例:

public static boolean trueExample() {
    return true;
}

public static void main(String[] args) {
    boolean returnValue = trueExample();
    System.out.println("trueExample() returned " + returnValue);
}

您还可以使其变得更复杂:

/**
 * @return the input value minus one.
 */
public static int oneLess(int num) {
    return num - 1;
}

public static void main(String[] args) {
    int num = 10;

    // Print 10
    System.out.println(num);

    // Print 9
    num = oneLess(num);
    System.out.println(num);

    // Print 8
    num = oneLess(num);
    System.out.println(num);
}

如果 num 位于以下位置,则该函数将返回 true范围 10-20 和 false 否则:

public boolean isValid(int num) {
    return num >= 10 && num <= 20;
}

此版本完全相同做同样的事情,但您可能会发现它更容易阅读,因为它更明确:

public boolean isValid(int num) {
    /* Numbers less than 10 are invalid */
    if(num < 10) {
        return false;
    }
    /* Numbers greater than 20 are invalid */
    else if (num > 20) {
        return false;
    }
    /* By process of elimination, everything else is valid */
    else {
        return true;
    }
}

如果不是,请告诉我足以帮助您解决问题。

Calling Private Methods

Calling a private method from a public one is exactly the same as calling any other method:

public void doStuff() {
    System.out.println("Stuff done.");
}

private void doOtherStuff() {
    System.out.println("Other stuff done.");
}

public void showHowItWorks() {
    doStuff();
    doOtherStuff();

    // or if you prefer this style:
    this.doStuff();
    this.doOtherStuff();
}

The important difference is that you can only call private methods from inside the class where they were defined:

class PublicExample {
    public static void doStuff() {
        System.out.println("Stuff done.");
    }
}

class PrivateExample {
    private static void doStuff() {
        System.out.println("Stuff done.");
    }
}

class Example {
    public static void Main(String[] args) {
        PublicExample.doStuff(); // Works
        PrivateExample.doStuff(); // Doesn't work (because it's private and defined in a different class)
    }
}

Return Values

private void isValid(int hour,  int minute) {
    if (hour >= 0 && hour <=23) {
        System.out.println("Hour is valid");
        hourIsValid = true;
    } else {
        System.out.println("Hour is not valid");
        hourIsValid = false;
        System.exit(0);
    }
}

The problem with this approach is that your program immediately dies if you input a bad hour or minute. What if I want to loop until I get a good input?

I think the main problem is that you don't know how to return a value from a method. Here's an example of a function that always returns true:

public static boolean trueExample() {
    return true;
}

public static void main(String[] args) {
    boolean returnValue = trueExample();
    System.out.println("trueExample() returned " + returnValue);
}

You can also make it more complicated:

/**
 * @return the input value minus one.
 */
public static int oneLess(int num) {
    return num - 1;
}

public static void main(String[] args) {
    int num = 10;

    // Print 10
    System.out.println(num);

    // Print 9
    num = oneLess(num);
    System.out.println(num);

    // Print 8
    num = oneLess(num);
    System.out.println(num);
}

Here's one that will return true if num is in the range 10-20 and false otherwise:

public boolean isValid(int num) {
    return num >= 10 && num <= 20;
}

This version does exactly the same thing, but you may find it easier to read since it's more explicit:

public boolean isValid(int num) {
    /* Numbers less than 10 are invalid */
    if(num < 10) {
        return false;
    }
    /* Numbers greater than 20 are invalid */
    else if (num > 20) {
        return false;
    }
    /* By process of elimination, everything else is valid */
    else {
        return true;
    }
}

Let me know if that's not enough to help you with your problem.

复古式 2024-12-19 20:34:37

就像您可以调用任何普通方法一样,只要私有方法对公共方法可见即可。

Just like you would call any normal method as long as the private method is visible to the public method.

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