[英]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.