[英]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();
}
}
}
我是一个完整的初学者,刚刚开始学习C#。 我尝试使用if和else语句创建一段代码。
当到达下面时,它使我有些混乱,并期望{ }
。
else (value = 57)
Console.WriteLine("Value is 57!");`
我该如何解决这个问题? 一个说明对初学者也很有帮助! 先感谢您。
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();
}
}
}
首先,您正在使用if ... else if ... else语句,而不是第二次使用else,而是在检查第一个条件是否为true时使用else if,如果不为true则为else if,第二个条件为真,那么如果上述条件中的任何一个都不为真,则最后我们使用else。 其次,为了进行比较,我们使用== not =
所以你的代码就像
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");
}
有关更多信息,请参见https://www.tutorialspoint.com/csharp/if_else_statement_in_csharp.htm上的控制语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.