试图正确确定数字是否怪异

时间:2018-12-25 03:29:43

标签: java methods output

制作一个可打印数字是否奇怪的程序。如果数字是奇数,那很奇怪。如果该数字在2到5之间(包括2和5),则并不奇怪。如果它在6到20之间(包括0和20),则很奇怪;如果它大于20,则很奇怪。我在这里遇到的问题是,我的输出不是显示“此数字很奇怪/不奇怪”,而是在一行上显示“ Weird”或“ Not Weird”,如果是偶数,则显示“ This number is 0”,或“此数字为1”(如果是奇数)。

public Weird(int num) 
{       
    n = num;
}

public int EvenOrOdd() 
{
    int check = n % 2;
    int answer = n / 2;
    if (check == 0 && answer >= 2 && answer <= 5) 
    {
        System.out.println("Not Weird");
    }

    else if (check == 0 && answer >= 6 && answer <= 20) 
    {
        System.out.println("Weird");
    }

    else if (check == 0 && answer > 20) 
    {
        System.out.println("Not Weird");
    }

    else if (check != 0) 
    {
        System.out.println("Weird");
    }
    return check;
}


public static void main(String[] args) 
{
    Weird w = new Weird(32);
    Weird a = new Weird(21);
    System.out.println("This number is " + w.EvenOrOdd());
    System.out.println("This number is " + a.EvenOrOdd());

}

3 个答案:

答案 0 :(得分:1)

您实际上返回了int字段的值check。这是1或0。

当您拨打此行时-

System.out.println("This number is " + w.EvenOrOdd());
System.out.println("This number is " + a.EvenOrOdd());

它打印This number is 0This number is 1

您可以通过两种方式获得所需的输出-

方法1- 将方法的返回类型更改为void EvenOrOdd()like-

public void EvenOrOdd() 
{
    int check = n % 2;
    int answer = n / 2;
    if (check == 0 && n >= 2 && n<= 5) 
    {
        System.out.println("Not Weird");
    }

    else if (check == 0 && n>= 6 && n<= 20) 
    {
        System.out.println("Weird");
    }

    else if (check == 0 && n> 20) 
    {
        System.out.println("Not Weird");
    }

    else if (check != 0) 
    {
        System.out.println("Weird");
    }
}

并在main as-

中调用方法
public static void main(String[] args) 
{
    Weird w = new Weird(32);
    Weird a = new Weird(21);
    w.EvenOrOdd();
    a.EvenOrOdd();

}

方法2-将方法的返回类型更改为String as-

public String EvenOrOdd() 
{
    int check = n % 2;
    int answer = n / 2;
    if (check == 0 && n>= 2 && n<= 5) 
    {
        return "Not Weird";
    }

    else if (check == 0 && n>= 6 && n<= 20) 
    {
        return "Weird";
    }

    else if (check == 0 && n> 20) 
    {
        return "Not Weird";
    }

    else
    {
        return "Weird";
    }
}

主要方法保持不变-

public static void main(String[] args) 
{
    Weird w = new Weird(32);
    Weird a = new Weird(21);
    System.out.println("This number is " + w.EvenOrOdd());
    System.out.println("This number is " + a.EvenOrOdd());

}

答案 1 :(得分:0)

从方法返回的值将“替代”方法调用。因为您返回了check

return check;

通话:

System.out.println("This number is " + w.EvenOrOdd());
System.out.println("This number is " + a.EvenOrOdd());

基本上是:

System.out.println("This number is " + 0);
System.out.println("This number is " + 1);

您似乎对退货和打印感到困惑。您的方法应如下所示:

public String EvenOrOdd() 
{
    int check = n % 2;
    int answer = n / 2;
    if (check == 0 && n >= 2 && n <= 5) 
    {
        return "Not Weird";
    }

    else if (check == 0 && n >= 6 && n <= 20) 
    {
        return "Weird";
    }

    else if (check == 0 && n > 20) 
    {
        return "Not Weird";
    }

    else
    {
        return "Weird";
    }
}

现在,这两个调用基本上是:

System.out.println("This number is " + "Weird");
System.out.println("This number is " + "Not Weird");

答案 2 :(得分:0)

您应该返回该字符串而不是打印它。在您的函数中,您将返回一个int,但您应该编写一个String。除此之外,一切看起来都不错,但是您可以减少支票的数量,如下所示:

public Weird(int num) 
{       
    n = num;
}

public String EvenOrOdd() 
{
    int check = n % 2;
    if (check == 1 || (check == 0 && n >= 6 && n <= 20)) {
        return "Weird";
    }else{
        return "Not Weird";
    }
}


public static void main(String[] args) 
{
    Weird w = new Weird(32);
    Weird a = new Weird(21);
    System.out.println("This number is " + w.EvenOrOdd());
    System.out.println("This number is " + a.EvenOrOdd());

}