简体   繁体   English

不确定我在这里缺少什么? C#(30 天代码挑战;条件语句介绍)

[英]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如果是奇数,则打印 Weird
If is even and in the inclusive range of to , print Not Weird如果是偶数并且在 to 的包含范围内,则打印 Not Weird
If is even and in the inclusive range of to , print Weird如果是偶数并且在 to 的包含范围内,则打印 Weird
If is even and greater than , print Not Weird如果是偶数且大于 ,则打印 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.输入格式:一行包含一个正整数 N。

Constraints: 1 <= n <= 100约束条件: 1 <= n <= 100

Output Format Print "Weird" if the number is weird;输出格式如果数字很奇怪就打印"Weird" otherwise, print "Not Weird"否则,打印"Not Weird"

Explanation: Sample Case 0: n=3说明:示例案例 0:n=3

n is odd and odd numbers are weird, so we print Weird n 是奇数,奇数很奇怪,所以我们打印 Weird

Sample Case 1: n = 24 n>20 and n is even, so it isn't weird.示例案例 1:n = 24 n>20 并且 n 是偶数,所以这并不奇怪。 Thus we print Not Weird.因此我们打印 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它通过了测试用例 0、1、4、5、6,但失败了 2、3 和 7。测试用例 2 是 4,什么也没有发生 测试用例 3 是 18,什么都没有发生 测试用例 7 是 20,什么也没有发生

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"情况 2 是 4 =“不奇怪” 情况 3 是 18 =“奇怪” 情况 7 是 20 =“奇怪”

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:您正在寻找 <= 6 AND >= 20 的数字。根据您的描述不确定,但听起来您可能需要切换顺序 - 例如:

        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.此外,由于您已经从第一个 if 语句中确定了数字是偶数,因此您可以稍微简化一下。

        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").首先检查输入的数字(本例为N)是否大于100小于0,然后声明字符串变量,同时用三元运算初始化,检查“是偶数还是奇数” (在这里我称她为“evenOdd”)。 If is even, then the value of "evenOdd" will be even and vice versa.如果是偶数,则“evenOdd”的值将是偶数,反之亦然。 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".因为任务要求是(如果​​是偶数并且 N >= 2 和 N <= 5 打印“Not Weird”,N >= 6 和 N <= 20 打印“Weird”,N >= 20 打印“Not Weird”并且还如果是“even”打印“Not Weird”和“odd”打印“Weird”,首先检查这个“if”里面的“evenOdd”是否是“even”,我做剩下的检查(N >= 2 or N < = 5 打印“Not Weird”等等)之后(evenOdd 是偶数)我再做一次检查(如果 evenOdd 是奇数打印“Weird”)。简而言之:N 如果是 6,将检查第一次检查并返回“false” ”,然后会进入“else”。因为6是“偶”,那么“evenOdd”的值会是“偶”,会进入第一个“if,因为在第一次检查时它大于5,返回false,因为它是 6,它将进入下一个检查,它返回 true 并打印“Weird”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# Hackerrank 代码在 30 天的代码中在数据类型挑战中产生了正确的结果,但说我错了 - C# Hackerrank code produces correct results on Data Types challenge in 30 days of code but says I'm wrong C#每隔30天运行一次代码 - C# run code every 30 days 我在这里缺少什么? IsSuccessStatusCode 无效 - What I'm missing here? IsSuccessStatusCode not valid Hackerrank 30 天的代码 - 第 8 天和 C# - 运行时错误 - Hackerrank 30 Days of Code - Day 8 & C# -Runtime Error C#我不确定下一步该怎么做才能从两个组合框中提取信息 - C# I'm not sure what to do next to pull the information from both comboboxes C#字符串相等运算符返回false,但我很确定它应该是真的......什么? - C# string equality operator returns false, but I'm pretty sure it should be true… What? 在C#中使用数据集时,datagridview上没有数据出现,我在这里缺少什么? - No data appear on my datagridview when using dataset in c#, what am i missing here? 不确定我是否在tftp app中正确使用“使用”c# - Not sure if I'm using “using” correctly c# in tftp app 指数数组的边界之外? 我不确定。 C# - Index was outside the bounds of the array? I'm not sure. C# 不知道为什么我会收到这个 C# 错误 - Not sure why I'm getting this C# error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM