我应该很清楚我想要完成什么,根据以前的选择制作不同的选项树。出于某种原因,它会在我留下评论的两个地方抛出错误“}。我做错了什么?这会不会像我想的那样工作?
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
double TotalAmount;
TotalAmount = 300.7 + 75.60;
Console.WriteLine("Your account currently contains this much money: {0:C} ", TotalAmount);
Console.WriteLine("Would you like to withdraw money? Please type yes or no.");
string UserInput2 = Console.ReadLine();
if (UserInput2.ToLower() == "yes") Console.WriteLine("How much would you like to withdraw?");
{
string UserInput = Console.ReadLine();
double UserInput3 = double.Parse(UserInput);
TotalAmount = TotalAmount - UserInput3;
Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount);
Console.WriteLine("Anything else?");
string UserInput4 = Console.ReadLine();
if (UserInput4.ToLower() == "yes") Console.WriteLine("What do you want to do?");
Console.WriteLine("1). Withdraw");
Console.WriteLine("2). Deposit");
Console.WriteLine("Use number key for selection");
string UserInput5 = Console.ReadLine();
if (UserInput5.ToLower() == "1") Console.WriteLine("How much would you like to withdraw?");
else if (UserInput5.ToLower() == "2") Console.WriteLine("How much would you like to deposit?");
string UserInput6 = Console.ReadLine(); //FIRST ERROR
else if (UserInput4.ToLower() == "no") Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
{
System.Environment.Exit(1);
}
} //SECOND ERROR
else if (UserInput2.ToLower() == "no") Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
{
System.Environment.Exit(1);
}
}
}
}
答案 0 :(得分:0)
您的问题是您的代码结构非常难以阅读,因此您在推理时遇到了问题。基本上,您没有正确关闭大括号。看看这个:
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
double TotalAmount;
TotalAmount = 300.7 + 75.60;
Console.WriteLine("Your account currently contains this much money: {0:C} ", TotalAmount);
Console.WriteLine("Would you like to withdraw money? Please type yes or no.");
string UserInput2 = Console.ReadLine();
string UserInput4;
if (UserInput2.ToLower() == "yes")
{
Console.WriteLine("How much would you like to withdraw?");
string UserInput = Console.ReadLine();
double UserInput3 = double.Parse(UserInput);
TotalAmount = TotalAmount - UserInput3;
Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount);
Console.WriteLine("Anything else?");
UserInput4 = Console.ReadLine();
if (UserInput4.ToLower() == "yes")
{
Console.WriteLine("What do you want to do?");
}
Console.WriteLine("1). Withdraw");
Console.WriteLine("2). Deposit");
Console.WriteLine("Use number key for selection");
string UserInput5 = Console.ReadLine();
if (UserInput5.ToLower() == "1")
{
Console.WriteLine("How much would you like to withdraw?");
}
else if (UserInput5.ToLower() == "2")
{
Console.WriteLine("How much would you like to deposit?");
}
string UserInput6 = Console.ReadLine();
}
else if (UserInput2.ToLower() == "no")
{
Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
System.Environment.Exit(1);
}
else if (UserInput2.ToLower() == "no") Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
{
System.Environment.Exit(1);
}
}
}
}
我建议每当你有一个“if”或“else if”语句时,总是把它后面的代码块放在大括号中 - 即使它是一行代码。它会为你节省很多痛苦。 另外,如果我在答案中搞砸了你的应用程序逻辑,那就很抱歉。但是你将你的变量命名为UserInput1,UserInput2等。如果你给出了一些更有意义的名字,例如“wouldLikeToWithdrawAnswer”,“howMuchAnswer”等,那会更容易。我只是在所有这些变量中有点迷失了:)
答案 1 :(得分:0)
你编写代码的方式很难被别人理解。 你需要改进很多。这是工作Example on .NET Fiddle
public static void Main()
{
double TotalAmount;
TotalAmount = 300.7 + 75.60;
Console.WriteLine("Your account currently contains this much money: {0:C} ", TotalAmount);
Console.WriteLine("Would you like to withdraw money? Please type yes or no.");
string UserInput2 = Console.ReadLine();
if (UserInput2.ToLower() == "yes")
{
Console.WriteLine("How much would you like to withdraw?");
string UserInput = Console.ReadLine();
double UserInput3 = double.Parse(UserInput);
TotalAmount = TotalAmount - UserInput3;
Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount);
Console.WriteLine("Anything else?");
string UserInput4 = Console.ReadLine();
if (UserInput4.ToLower() == "yes")
{
Console.WriteLine("What do you want to do?");
Console.WriteLine("1). Withdraw");
Console.WriteLine("2). Deposit");
Console.WriteLine("Use number key for selection");
string UserInput5 = Console.ReadLine();
if (UserInput5.ToLower() == "1")
{
Console.WriteLine("How much would you like to withdraw?");
}
else if (UserInput5.ToLower() == "2")
{
Console.WriteLine("How much would you like to deposit?");
string UserInput6 = Console.ReadLine(); //FIRST ERROR
}
}
else if (UserInput4.ToLower() == "no")
{
Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
System.Environment.Exit(1);
}
} //SECOND ERROR
else if(UserInput2.ToLower() == "no")
{
Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
System.Environment.Exit(1);
}
}
答案 2 :(得分:0)
问题是你的if
阻止不在您认为的位置:
if (UserInput2.ToLower() == "yes") Console.WriteLine("How much would you like to withdraw?");
// And that's the if statement done.
{
// This is not part of any if block
string UserInput = Console.ReadLine();
double UserInput3 = double.Parse(UserInput);
TotalAmount = TotalAmount - UserInput3;
Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount);
Console.WriteLine("Anything else?");
string UserInput4 = Console.ReadLine();
if (UserInput4.ToLower() == "yes") Console.WriteLine("What do you want to do?");
Console.WriteLine("1). Withdraw");
Console.WriteLine("2). Deposit");
Console.WriteLine("Use number key for selection");
string UserInput5 = Console.ReadLine();
if (UserInput5.ToLower() == "1") Console.WriteLine("How much would you like to withdraw?");
else if (UserInput5.ToLower() == "2") Console.WriteLine("How much would you like to deposit?");
string UserInput6 = Console.ReadLine(); //FIRST ERROR
else //Compiler says "else? There is no if to match this with!"