为什么在我输入 yes 后控制台关闭?
Why does the console close after I type yes?
我正在用 C# 制作一个计算器。这是乘法部分---
using System;
static class calculator
{
public static void Main()
{
welcome:
Console.WriteLine("Welcome to my calculator, please press enter to continue");
Console.ReadLine();
Console.WriteLine("Do you want to add, subtract, multiply or divide?");
string x = Convert.ToString(Console.ReadLine());
if (x == "multiply")
{
Console.WriteLine("Please enter the first value");
decimal value1multiply = Convert.ToDecimal( Console.ReadLine());
Console.WriteLine("Please enter the second value" );
decimal value2multiply = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Result =");
Console.WriteLine(value1multiply * value2multiply);
Console.WriteLine("Thank you for using my calculator!Do you still want to use it?Please answer in 'yes' or 'no' and press 'enter'");
Console.ReadLine();
string yesorno =Console.ReadLine();
if (yesorno == "yes")
{
goto welcome;
}
if (yesorno == "no")
{
Environment.Exit(0);
}
}
}
}
当我输入 'yes' 时,控制台会引导我欢迎。但是,它并没有带我到任何地方,而是一片空白。当我再次按下回车键时,控制台关闭。为什么会发生这种情况,我该如何预防?
我尝试过的:
我尝试删除 environment.exit(0),认为控制台引导我这样做,但没有帮助。我什至尝试在 Visual Studio 中输入代码,但结果没有区别。 (我用的是sharp develop)
除了经常不受欢迎的 goto
之外,您还使用了太多 ReadLine
调用。
这里:
Console.ReadLine();
string yesorno = Console.ReadLine();
也许您输入了 yes
,然后按了两次 Enter。在那种情况下 yesorno
将为空,您的检查将失败。第一个条目被未分配给变量的第一个 ReadLine
吞没。
我正在用 C# 制作一个计算器。这是乘法部分---
using System;
static class calculator
{
public static void Main()
{
welcome:
Console.WriteLine("Welcome to my calculator, please press enter to continue");
Console.ReadLine();
Console.WriteLine("Do you want to add, subtract, multiply or divide?");
string x = Convert.ToString(Console.ReadLine());
if (x == "multiply")
{
Console.WriteLine("Please enter the first value");
decimal value1multiply = Convert.ToDecimal( Console.ReadLine());
Console.WriteLine("Please enter the second value" );
decimal value2multiply = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Result =");
Console.WriteLine(value1multiply * value2multiply);
Console.WriteLine("Thank you for using my calculator!Do you still want to use it?Please answer in 'yes' or 'no' and press 'enter'");
Console.ReadLine();
string yesorno =Console.ReadLine();
if (yesorno == "yes")
{
goto welcome;
}
if (yesorno == "no")
{
Environment.Exit(0);
}
}
}
}
当我输入 'yes' 时,控制台会引导我欢迎。但是,它并没有带我到任何地方,而是一片空白。当我再次按下回车键时,控制台关闭。为什么会发生这种情况,我该如何预防?
我尝试过的:
我尝试删除 environment.exit(0),认为控制台引导我这样做,但没有帮助。我什至尝试在 Visual Studio 中输入代码,但结果没有区别。 (我用的是sharp develop)
除了经常不受欢迎的 goto
之外,您还使用了太多 ReadLine
调用。
这里:
Console.ReadLine();
string yesorno = Console.ReadLine();
也许您输入了 yes
,然后按了两次 Enter。在那种情况下 yesorno
将为空,您的检查将失败。第一个条目被未分配给变量的第一个 ReadLine
吞没。