Trying to correctly determine whether numbers are weird

问题: Made a program that prints if a number is weird or not. If the number is odd, it's weird. If the number is even and inclusively between 2 and 5, it is not weird. If it's ev...

问题:

Made a program that prints if a number is weird or not. If the number is odd, it's weird. If the number is even and inclusively between 2 and 5, it is not weird. If it's even and inclusively between 6 and 20, it's weird, and if it's even and greater than 20, it's not weird. The problem I'm having here is that instead of the output displaying "This number is weird/not weird", I get "Weird" or "Not Weird" on one line, followed by "This number is 0" if it's even, or "This number is 1" if it's odd.

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());

}

回答1:

You should return that string rather than printing it. In your function, you are returning an int but rather you should write a String. Besides that everything looks good but you can decrease the number of check like below:

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());

}

回答2:

You actually return int which is value of check field. This is either 1 or 0.

When you call this line-

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

It prints either This number is 0 or This number is 1.

You can get desired output by two ways-

Way 1- Change return type of method to 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");
    }
}

and call method in main as-

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

}

Way 2- Change return type of method to 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";
    }
}

And main method remains same-

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:

The value you return from a method will kind of "replace" the method call. Because you returned check:

return check;

The calls:

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

are basically:

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

You seem to be confused about returning and printing. Your method should look like this:

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";
    }
}

Now the two calls is basically:

System.out.println("This number is " + "Weird");
System.out.println("This number is " + "Not Weird");
  • 发表于 2018-12-27 11:16
  • 阅读 ( 226 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除