简体   繁体   English

如果以及其他代码,我该如何修复我的初学者?

[英]How can i fix my beginner if and else code?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyFirstTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32 value = 57;

            if (value < 10)

                Console.WriteLine("Value is less than 10");

            else (value = 57)

                Console.WriteLine("Value is 57!");

            else

                Console.WriteLine("Value is greater than 10");


            Console.ReadLine();
        }
    }
}

I am a complete beginner and have just started learning C#. 我是一个完整的初学者,刚刚开始学习C#。 I have tried to create a snippet of code using the if and else statements. 我尝试使用if和else语句创建一段代码。

When it gets to the below, it throws me some squiggly lines and expects { } . 当到达下面时,它使我有些混乱,并期望{ }

else (value = 57) 
Console.WriteLine("Value is 57!");`

How can i go about fixing this? 我该如何解决这个问题? An explanation would also be great to help a beginner! 一个说明对初学者也很有帮助! Thank you in advance. 先感谢您。

namespace MyFirstTest
{
   class Program
   {
       static void Main(string[] args)
       {
           Int32 value = 57;

           if (value < 10)
           {
               Console.WriteLine("Value is less than 10");
           }
           else if(value == 57)
           {
               Console.WriteLine("Value is 57!");
           }
           else
           {
               Console.WriteLine("Value is greater than 10");
           }

           Console.ReadLine();
       }
   }
}

Firstly you are using if...else if...else Statement, rather than using else for the second time, you have to use else if as you are checking like if the first condition is true, if not true then else if, second condition is true then at last we use else, if any of the above mentioned conditions are not true. 首先,您正在使用if ... else if ... else语句,而不是第二次使用else,而是在检查第一个条件是否为true时使用else if,如果不为true则为else if,第二个条件为真,那么如果上述条件中的任何一个都不为真,则最后我们使用else。 Secondly, to compare, we use == not = 其次,为了进行比较,我们使用== not =

So here your code goes like 所以你的代码就像

if(value < 10)
{ 
    Console.WriteLine("Value is less than 10");
}
else if(value == 57)
{ 
    Console.WriteLine("Value is 57!");
}
else
{ 
    Console.WriteLine("Value is greater than 10");
}

For more information see control statements at https://www.tutorialspoint.com/csharp/if_else_statement_in_csharp.htm 有关更多信息,请参见https://www.tutorialspoint.com/csharp/if_else_statement_in_csharp.htm上的控制语句。

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

相关问题 如何修复代码中的 SqlException 不正确的语法错误? - How can I fix a SqlException incorrect syntax error in my code? 我的代码有效,但我的程序无效,我该如何解决? - My code works but my program doesn't, How can I fix this? (C#) 我怎样才能使所有这些“else if”语句更简洁,我怎样才能更好地优化我的代码? - (C#) How can I make all these "else if" statements more condensed and how can I optimize my code better? 为什么我的代码说我正在尝试将十进制转换为双精度,我该如何解决? - Why does my code say that I'm trying to convert decimal to double and how can I fix it? 如何为WPF应用程序修复此代码? - How can I fix this code for WPF Application? 我该如何解决我的状况 - How can I fix my condition 如何修复未在C#代码中设置为对象实例的对象引用 - How can I fix Object reference not set to an instance of an object in my C# code 如何修复我的代码以将已存在的文件移动到另一个目录? - How can I fix my code to move files that already exist to another directory? 当卡巴斯基安全软件警告病毒我的代码被阻止时,我该如何解决? - How can i fix when Kaspersky Internet Security alert virus my code block? 我在 Unity 中工作,我的 Punch 和 Jump 动画没有完全播放。 如何修复我的代码? - I am working in Unity and my Punch and jump animations don't play fully. How can I fix my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM