在控制台应用程序中创建选项树
Creating option trees in console applications
应该很清楚我要完成什么,根据以前的选择制作不同的选项树。出于某种原因,它在我留下评论的两个地方都抛出了错误“}。我做错了什么吗?这会像我想要的那样工作吗?
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);
}
}
}
}
你的问题是你的代码结构很难阅读,因此你在推理时遇到了问题。基本上,您没有正确关闭大括号。看看这个:
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" 等,会容易得多。我只是对所有这些变量有点迷茫:)
别人很难理解您编写代码的方式。
你需要提高很多。这是工作 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);
}
}
问题是您的 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!"
应该很清楚我要完成什么,根据以前的选择制作不同的选项树。出于某种原因,它在我留下评论的两个地方都抛出了错误“}。我做错了什么吗?这会像我想要的那样工作吗?
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);
}
}
}
}
你的问题是你的代码结构很难阅读,因此你在推理时遇到了问题。基本上,您没有正确关闭大括号。看看这个:
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" 等,会容易得多。我只是对所有这些变量有点迷茫:)
别人很难理解您编写代码的方式。 你需要提高很多。这是工作 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);
}
}
问题是您的 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!"