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