简体   繁体   中英

Not sure what I'm missing here? C#(30 Days of Code Challenge; Intro to Conditional Statements)

Here is the problem:

Given an integer, , perform the following conditional actions:

If is odd, print Weird
If is even and in the inclusive range of to , print Not Weird
If is even and in the inclusive range of to , print Weird
If is even and greater than , print Not Weird

Complete the stub code provided in your editor to print whether or not is weird.

Input Format: A single line containing a positive integer, N.

Constraints: 1 <= n <= 100

Output Format Print "Weird" if the number is weird; otherwise, print "Not Weird"

Explanation: Sample Case 0: n=3

n is odd and odd numbers are weird, so we print Weird

Sample Case 1: n = 24 n>20 and n is even, so it isn't weird. Thus we print Not Weird.

I've done stuff like this before but I'm not sure what is missing.

It passes Test cases 0,1,4,5,6 but fails 2, 3,and 7. Test Case 2 is 4 and nothing happens Test Case 3 is 18 and nothing happens Test Case 7 is 20 and nothing happens

Console.WriteLine("Enter:");
int N = Convert.ToInt32(Console.ReadLine());

if (N % 2 == 1)
{
    Console.WriteLine("Weird");
}
else if (N % 2 == 0 && (N <= 2 && N >= 5))
{
    Console.WriteLine("Not Weird");
}
else if (N % 2 == 0 && (N <= 6 && N >= 20))
{
    Console.WriteLine("Weird");
}

else if (N % 2 == 0 && N > 20)
{
    Console.WriteLine("Not Weird");
}

Case 2 is 4 = "Not Weird" Case 3 is 18 = "Weird" Case 7 is 20 = "Weird"

Your cases are impossible. For example...

        else if (N % 2 == 0 && (N <= 6 && N >= 20))
        {
            Console.WriteLine("Weird");
        }

will never happen. You're looking for numbers that are <= 6 AND >= 20. Not sure from your description, but it sounds like you might need to switch the order - example:

        else if (N % 2 == 0 && (N >= 6 && N <= 20))
        {
            Console.WriteLine("Weird");
        }

Furthermore, since you've already determined the number is even from the first if statement, you can simplify a little bit.

        else if (N >= 6 && N <= 20)
        {
            Console.WriteLine("Weird");
        }

Try this:

if(N <= 1|| N >= 100){
        Console.WriteLine("Number can not be less then 1 and bigger then 100");
    }
    else{
        string evenOdd = N % 2 == 0 ? "even" : "odd";
        if(evenOdd == "even"){
            if(N >=2 || N <= 5){
                Console.WriteLine("Not Weird");
            }
            else if(N >= 6 || N <= 20){
                Console.WriteLine("Weird");
            }
            else{
                Console.WriteLine("Not Weird");
            }
            
        } 
        if(evenOdd == "odd"){
            Console.WriteLine("Weird");
        }
    }

First check if the input number (in this case is N) is greater than 100 and less than 0. After that, declare string variable and at the same time initialize with ternary operation, which makes a check "whether it is even or odd" (here I called her "evenOdd"). If is even, then the value of "evenOdd" will be even and vice versa. Because the task requirement is (if is even and N >= 2 and N <= 5 print "Not Weird", N >= 6 and N <= 20 print "Weird", N >= 20 print "Not Weird" and also if it is "even" print "Not Weird" and "odd" print "Weird", first check if "evenOdd" is "even" inside in this "if", I do the remaining checks (N >= 2 or N <= 5 print "Not Weird" and so on) and after (evenOdd is even) I do another check (if evenOdd is odd print "Weird"). In short: N if is 6, will check the first check and return "false", then will enter in "else". Because 6 is "even", then the value of "evenOdd will be "even" and will enter the first "if, because in the first check it is greater than 5, return false, since it is 6, it will enter the next check, which returns true and will print "Weird".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM