每次插入不是39的尺寸时,控制台不显示任何内容,我该如何解决这个问题?

问题描述 投票:0回答:1
using System;

namespace deez
{
 class Hello
 {
    static void Main(string[] args)
    {
            int size = 39;
            string color = "black";
            if (size == 40 && (color == "black" || color == "blue"))
            {
                Console.WriteLine("ok, i'm gonna take it");
            }
            else if (size <= 38 )    
            {
                Console.WriteLine("it's a little too small but i'm gonna take it anyways");
            }
            else if(size >= 40)
            {
                Console.WriteLine("it's a little too big but i'm gonna take it anyways");
            }
            else if ((color != "black" || color != "blue") && (size <= 38 || size >= 40))
            {
                Console.WriteLine("the size is fine but i would prefer another color");
            }

            Console.ReadLine();
    }

 }
}

每次我运行它时,就像其他

else if
不存在一样,我不知道如何解决这个问题,我就是无法让它工作。它唯一一次起作用是当我使用前面没有
else
if
时,但只能显示类似的通用按摩:“嗯,我想我不会再买这个了”,但我会更喜欢更具体的东西。

c# if-statement console-application
1个回答
0
投票

我将您的代码封装在

Test
方法中,并使用不同的
size
值调用它:

using System;
                    
public class Program
{
    public static void Main()
    {
        for(int i = 35; i<=45; i++)
        {
            Test(i);
        }
    }

    static void Test(int size)
    {
        Console.Write("Test with {0}:", size);
        string color = "black";
        if (size == 40 && (color == "black" || color == "blue"))
        {
            Console.WriteLine("ok, i'm gonna take it");
        }
        else if (size <= 38 )    
        {
            Console.WriteLine("it's a little too small but i'm gonna take it anyways");
        }
        else if(size >= 40)
        {
            Console.WriteLine("it's a little too big but i'm gonna take it anyways");
        }
        else if ((color != "black" || color != "blue") && (size <= 38 || size >= 40))
        {
            Console.WriteLine("the size is fine but i would prefer another color");
        }
        else
        {
            Console.WriteLine();
        }
    }
}

这是结果:

Test with 35: it's a little too small but i'm gonna take it anyways
Test with 36: it's a little too small but i'm gonna take it anyways
Test with 37: it's a little too small but i'm gonna take it anyways
Test with 38: it's a little too small but i'm gonna take it anyways
Test with 39:
Test with 40: ok, i'm gonna take it
Test with 41: it's a little too big but i'm gonna take it anyways
Test with 42: it's a little too big but i'm gonna take it anyways
Test with 43: it's a little too big but i'm gonna take it anyways
Test with 44: it's a little too big but i'm gonna take it anyways
Test with 45: it's a little too big but i'm gonna take it anyways

我真的不明白为什么你似乎说当你选择与 39 不同的东西时没有输出。对我来说,看起来恰恰相反

© www.soinside.com 2019 - 2024. All rights reserved.