我用 C# 编写了一个计算器程序。
如果用户在我的 C# 控制台应用程序中输入字符“x”,我希望我的程序退出(结束)。
我试过使用return;,但我没有让它工作。
也试图打电话Environment.Exit(),但也没有让它工作。
如果有人有任何提示,请随时分享!:)
下面是处理用户输入的代码:
Console.Write("Skriv in en siffra: ");
double firstUserInput;
while (!Double.TryParse(Console.ReadLine(), out firstUserInput))
{
Console.WriteLine();
Console.Write("Ogiltigt värde, skriv in en siffra: ");
}
while (true)
{
Console.WriteLine();
Console.Write("Välj räknesätt mellan + - * / : ");
string operators = Console.ReadLine();
Console.WriteLine();
Console.Write("Skriv in en till siffra: ");
double secondUserInput;
while (!Double.TryParse(Console.ReadLine(), out secondUserInput))
{
Console.WriteLine();
Console.Write("Ogiltigt värde, skriv in en siffra: ");
}
Console.WriteLine();
Console.WriteLine();
switch (operators)
{
case "+":
Console.WriteLine();
Console.WriteLine("Svar: " + firstUserInput + " + " + secondUserInput + " = " + (firstUserInput + secondUserInput));
firstUserInput = (firstUserInput + secondUserInput);
Console.ReadLine();
Console.WriteLine();
break;
case "-":
Console.WriteLine();
Console.WriteLine("Svar: " + firstUserInput + " - " + secondUserInput + " = " + (firstUserInput - secondUserInput));
firstUserInput = (firstUserInput - secondUserInput);
Console.ReadLine();
Console.WriteLine();
break;
case "*":
Console.WriteLine();
Console.WriteLine("Svar: " + firstUserInput + " * " + secondUserInput + " = " + (firstUserInput * secondUserInput));
firstUserInput = (firstUserInput * secondUserInput);
Console.ReadLine();
Console.WriteLine();
break;
case "/":
if (secondUserInput == 0)
{
Console.WriteLine();
Console.WriteLine("Går inte att dividera med 0, försök igen!");
Console.ReadLine();
Console.WriteLine();
}
else
{
Console.WriteLine();
Console.WriteLine("Svar: " + firstUserInput + " / " + secondUserInput + " = " + (firstUserInput / secondUserInput));
firstUserInput = (firstUserInput / secondUserInput);
Console.ReadLine();
Console.WriteLine();
}
break;
}
}
}