【问题标题】:How do i write an if statement to be between two values?我如何编写介于两个值之间的 if 语句?
【发布时间】:2022-12-22 22:06:27
【问题描述】:

这是我的代码,对于我的 if 语句,我希望它位于两个值之间,例如:

if(rating < 5 > 2);

所以我说我希望它只在该值低于 5 但高于 2 时打印命令。 有没有办法做到这一点?感谢您的时间。

这是我的代码。

 public static void Main(string[] args)
        {
            Console.WriteLine("What would you rate starcraft out of 10?");
            int rating = Console.Read();


            if (rating < 5) ;
            {
                Console.WriteLine("Not good enough!");
                Console.ReadLine();
            }

            if (rating > 5) ;
            {
                Console.WriteLine("OOOOOOOOO yeeeeeeee");
                Console.ReadLine();

            }
            if (rating > 8) ;
            {
                Console.WriteLine("We are bestfriends now ;)");
                Console.ReadLine();

【问题讨论】:

    标签: c# if-statement


    【解决方案1】:

    使用conditional logical AND operator &amp;&amp;

    if (rating < 5 && rating > 2)
    {
    }
    

    或者模式匹配:

    if (rating is < 5 and > 2)
    {    
    }
    

    【讨论】:

    • 出色的!谢谢你! :)
    猜你喜欢
    • 2021-05-04
    • 2016-08-30
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多