C# 错误 ;4 ;无效的表达式项';其他';
我见过其他问题,但我是c的初学者# 代码:C# 错误 ;4 ;无效的表达式项';其他';,c#,syntax-error,C#,Syntax Error,我见过其他问题,但我是c的初学者# 代码: 您不应该在else后面加分号,并且在else前面缺少一个结束符 namespace Input_Program { class Program { static void Main() { char Y = Console.ReadKey().KeyChar; Console.WriteLine("Welcome to my bool program!");
您不应该在else后面加分号,并且在else前面缺少一个结束符
namespace Input_Program
{
class Program
{
static void Main()
{
char Y = Console.ReadKey().KeyChar;
Console.WriteLine("Welcome to my bool program!");
Console.WriteLine("Input a NON capital y or n when told to.");
if(Y == 'y')
{
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
}
else
{
if(Y == 'n')
{
Console.WriteLine("You input N");
}
}
Console.ReadLine();
}
}
}
您不应该在else后面加分号,并且在else前面缺少一个结束符
namespace Input_Program
{
class Program
{
static void Main()
{
char Y = Console.ReadKey().KeyChar;
Console.WriteLine("Welcome to my bool program!");
Console.WriteLine("Input a NON capital y or n when told to.");
if(Y == 'y')
{
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
}
else
{
if(Y == 'n')
{
Console.WriteLine("You input N");
}
}
Console.ReadLine();
}
}
}
在由
if
控制的两条语句之后(就在else
之前),需要一个右大括号}
。此外,完成此操作后,请删除else
后面的分号。在由if
控制的两个语句之后(就在else
之前),需要一个右括号}
。此外,完成此操作后,请删除else
后面的分号
{
char Y = Console.ReadKey().KeyChar;
Console.WriteLine("Welcome to my bool program!")
Console.WriteLine("Input a NON capital y or n when told to.");
if(Y == 'y')
{
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
}else
{
if(Y == 'n')
{
Console.WriteLine("You input N");
}
}
Console.ReadLine();
}
匹配缩进可以帮助您看到需要/不需要闭合的花括号,并且不需要在else后面加分号
namespace Input_Program
{
class Program
{
static void Main()
{
char Y = Console.ReadKey().KeyChar;
Console.WriteLine("Welcome to my bool program!");
Console.WriteLine("Input a NON capital y or n when told to.");
if(Y == 'y')
{
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
}
else
{
if(Y == 'n')
{
Console.WriteLine("You input N");
}
}
Console.ReadLine();
}
}
}
您还可以将结构更改为if/else,或两个if,以获得相同的结果
{
char Y = Console.ReadKey().KeyChar;
Console.WriteLine("Welcome to my bool program!")
Console.WriteLine("Input a NON capital y or n when told to.");
if(Y == 'y')
{
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
}
if(Y == 'n')
{
Console.WriteLine("You input N");
}
Console.ReadLine();
}
匹配缩进可以帮助您看到需要/不需要闭合的花括号,并且不需要在else后面加分号
namespace Input_Program
{
class Program
{
static void Main()
{
char Y = Console.ReadKey().KeyChar;
Console.WriteLine("Welcome to my bool program!");
Console.WriteLine("Input a NON capital y or n when told to.");
if(Y == 'y')
{
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
}
else
{
if(Y == 'n')
{
Console.WriteLine("You input N");
}
}
Console.ReadLine();
}
}
}
您还可以将结构更改为if/else,或两个if,以获得相同的结果
{
char Y = Console.ReadKey().KeyChar;
Console.WriteLine("Welcome to my bool program!")
Console.WriteLine("Input a NON capital y or n when told to.");
if(Y == 'y')
{
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
}
if(Y == 'n')
{
Console.WriteLine("You input N");
}
Console.ReadLine();
}
else
后面没有分号,您需要合上大括号。重做语法:
if(Y == 'y'){
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
}
else{
if(Y == 'n'){
Console.WriteLine("You input N");
}
}
编辑:
else{if(Y=='n'){…}
最好重构为else,如果(Y=='n'){…}
else
后面没有分号,您需要关闭大括号。重做语法:
if(Y == 'y'){
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
}
else{
if(Y == 'n'){
Console.WriteLine("You input N");
}
}
编辑:else{if(Y='n'){…}
重构为else如果(Y='n'){…}
我注意到两件事
if(Y == 'y')
{
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
// you need to close the if with a } on this line
else; //no semicolon after else
{
if(Y == 'n')
{
Console.WriteLine("You input N");
}
}
}
我注意到两件事
if(Y == 'y')
{
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
// you need to close the if with a } on this line
else; //no semicolon after else
{
if(Y == 'n')
{
Console.WriteLine("You input N");
}
}
}
任何使用大括号的东西都是“代码块”。定义
类
时,在花括号内定义一块代码,如下所示:
public class MyClass
{
}
大括号内的内容被视为“在该范围内”。例如:
public class MyClass
{
// This method is in the scope of MyClass
public void MyMethod()
{
// This variable is in the scope of MyMethod.
// It is only accessible from within this method because that is where
// it is defined.
string myString = "Hello Method.";
}
// This variable is in the scope of MyClass.
// It is accessible within MyClass, including all methods that are also in scope
// of MyClass
public string myGlobalString = "Hello Global.";
}
public class MyClass
{
public void MyMethod(string myString)
{
if (myString == "Hello")
{
// Read like English. "If variable myString equals the value 'Hello', then do this code within this block."
}
else if (myString == "Goodbye")
{
// "...or else if variable myString equals the value 'Goodbye', then do this code within this block instead.
}
else if (myString == "Good Morning")
{
// "...or else if variable myString equals the value 'Good Morning', then do this code within this block instead.
}
else
{
// "...or else do this code if variable myString does not match any of the above code statements.
}
// You are not required to include "else" or even "else if". You could just do a single IF statement like this:
if (myString == "hi)
{
// "If variable myString equals the value 'hi' then do this, otherwise do nothing."
}
}
}
对于IF
语句,它们是一系列块语句,只能包含在方法范围内(例如,在类中不能使用IF
语句)。代码只能在语句提供的给定路径中的一条路径上运行。例如:
public class MyClass
{
// This method is in the scope of MyClass
public void MyMethod()
{
// This variable is in the scope of MyMethod.
// It is only accessible from within this method because that is where
// it is defined.
string myString = "Hello Method.";
}
// This variable is in the scope of MyClass.
// It is accessible within MyClass, including all methods that are also in scope
// of MyClass
public string myGlobalString = "Hello Global.";
}
public class MyClass
{
public void MyMethod(string myString)
{
if (myString == "Hello")
{
// Read like English. "If variable myString equals the value 'Hello', then do this code within this block."
}
else if (myString == "Goodbye")
{
// "...or else if variable myString equals the value 'Goodbye', then do this code within this block instead.
}
else if (myString == "Good Morning")
{
// "...or else if variable myString equals the value 'Good Morning', then do this code within this block instead.
}
else
{
// "...or else do this code if variable myString does not match any of the above code statements.
}
// You are not required to include "else" or even "else if". You could just do a single IF statement like this:
if (myString == "hi)
{
// "If variable myString equals the value 'hi' then do this, otherwise do nothing."
}
}
}
任何使用大括号的东西都是“代码块”。定义
类
时,在花括号内定义一块代码,如下所示:
public class MyClass
{
}
大括号内的内容被视为“在该范围内”。例如:
public class MyClass
{
// This method is in the scope of MyClass
public void MyMethod()
{
// This variable is in the scope of MyMethod.
// It is only accessible from within this method because that is where
// it is defined.
string myString = "Hello Method.";
}
// This variable is in the scope of MyClass.
// It is accessible within MyClass, including all methods that are also in scope
// of MyClass
public string myGlobalString = "Hello Global.";
}
public class MyClass
{
public void MyMethod(string myString)
{
if (myString == "Hello")
{
// Read like English. "If variable myString equals the value 'Hello', then do this code within this block."
}
else if (myString == "Goodbye")
{
// "...or else if variable myString equals the value 'Goodbye', then do this code within this block instead.
}
else if (myString == "Good Morning")
{
// "...or else if variable myString equals the value 'Good Morning', then do this code within this block instead.
}
else
{
// "...or else do this code if variable myString does not match any of the above code statements.
}
// You are not required to include "else" or even "else if". You could just do a single IF statement like this:
if (myString == "hi)
{
// "If variable myString equals the value 'hi' then do this, otherwise do nothing."
}
}
}
对于IF
语句,它们是一系列块语句,只能包含在方法范围内(例如,在类中不能使用IF
语句)。代码只能在语句提供的给定路径中的一条路径上运行。例如:
public class MyClass
{
// This method is in the scope of MyClass
public void MyMethod()
{
// This variable is in the scope of MyMethod.
// It is only accessible from within this method because that is where
// it is defined.
string myString = "Hello Method.";
}
// This variable is in the scope of MyClass.
// It is accessible within MyClass, including all methods that are also in scope
// of MyClass
public string myGlobalString = "Hello Global.";
}
public class MyClass
{
public void MyMethod(string myString)
{
if (myString == "Hello")
{
// Read like English. "If variable myString equals the value 'Hello', then do this code within this block."
}
else if (myString == "Goodbye")
{
// "...or else if variable myString equals the value 'Goodbye', then do this code within this block instead.
}
else if (myString == "Good Morning")
{
// "...or else if variable myString equals the value 'Good Morning', then do this code within this block instead.
}
else
{
// "...or else do this code if variable myString does not match any of the above code statements.
}
// You are not required to include "else" or even "else if". You could just do a single IF statement like this:
if (myString == "hi)
{
// "If variable myString equals the value 'hi' then do this, otherwise do nothing."
}
}
}
尝试了分号技巧,但它说它是预期的;在elseJust之后,因为编译器说它期望某些东西,并不意味着正确的答案是将它放在那里。:)您的右括号放错了位置。删除
}
控制台前面的命令代码>@Chevex:我删除了违规的}。谢谢尝试了分号技巧,但它说它是预期的;在elseJust之后,因为编译器说它期望某些东西,并不意味着正确的答案是将它放在那里。:)您的右括号放错了位置。删除}
控制台前面的命令代码>@Chevex:我删除了违规的}。谢谢我知道你是个初学者,你正在学习。有一件事,你想嵌入到你的脑海中,那就是保持格式的一致性。每次使用左大括号{
开始一个新块时,应将下面的每一行缩进一个附加级别,并保持所有内容对齐,直到到达匹配的右大括号}
。如果您这样做了,您就可以很容易地看到您的代码存在问题。大括号不匹配,语句不应该出现,等等。Visual Studio甚至可以帮助您进行缩进,只要您遵循它。您可以使用CTRL+K,D自动重新格式化文档。我知道您是初学者,正在学习。有一件事,你想嵌入到你的脑海中,那就是保持格式的一致性。每次使用左大括号{
开始一个新块时,应将下面的每一行缩进一个附加级别,并保持所有内容对齐,直到到达匹配的右大括号}
。如果您这样做了,您就可以很容易地看到您的代码存在问题。大括号不匹配,语句不应该出现在语句中,等等。只要遵循它,Visual Studio甚至可以帮助您进行缩进。您可以使用CTRL+K,D自动重新格式化文档。