如何在 C# 中使用两个 if 语句?
How to use two if statements in C#?
这是我的 ATM 代码,但不知何故,当我在小键盘上按数字 2 时,我得到了 else
声明,说没有这个号码的选择。
Console.WriteLine("What is your name? ");
string userName = Console.ReadLine();
Console.WriteLine("You are: " + userName);
Console.WriteLine("How much money do you have? ");
string Balance = Console.ReadLine();
float startBalance = float.Parse(Balance);
Console.WriteLine(userName + ", your balance is " + startBalance + " EUR");
Console.WriteLine("Press 1 for WITHDRAWAL");
Console.WriteLine("Press 2 for DEPOSIT");
if (Console.ReadKey().Key == ConsoleKey.NumPad1)
{
Console.ReadLine();
Console.WriteLine("How much money do you wish to withdraw? ");
string Withdrawal = Console.ReadLine();
float wBalance = float.Parse(Withdrawal);
Console.WriteLine("Your new balance is " + (startBalance - wBalance) + " EUR");
Console.ReadLine();
Environment.Exit(0);
}
if (Console.ReadKey().Key == ConsoleKey.NumPad2)
{
Console.ReadLine();
Console.WriteLine("How much money do you wish to deposit? ");
string Deposit = Console.ReadLine();
float dBalance = float.Parse(Deposit);
Console.WriteLine("Your new balance is " + (startBalance - dBalance) + " EUR");
Console.ReadLine();
Environment.Exit(0);
}
else Console.WriteLine("There was no choice for this number");
您有两个单独的 if
和两个单独的 Console.ReadKey()
.
调用
相反,将该调用提取到一个变量中,并使用 if-else if-else 序列对其求值:
ConsoleKeyInfo.Key key = onsole.ReadKey().Key;
if (key == ConsoleKey.NumPad1)
{
Console.ReadLine();
Console.WriteLine("How much money do you wish to withdraw? ");
string Withdrawal = Console.ReadLine();
float wBalance = float.Parse(Withdrawal);
Console.WriteLine("Your new balance is " + (startBalance - wBalance) + " EUR");
Console.ReadLine();
Environment.Exit(0);
}
else if (key == ConsoleKey.NumPad2)
{
Console.ReadLine();
Console.WriteLine("How much money do you wish to deposit? ");
string Deposit = Console.ReadLine();
float dBalance = float.Parse(Deposit);
Console.WriteLine("Your new balance is " + (startBalance - dBalance) + " EUR");
Console.ReadLine();
Environment.Exit(0);
}
else
{
Console.WriteLine("There was no choice for this number");
}
复制+粘贴是邪恶:请注意减去(添加预期)存款
"Your new balance is " + (startBalance - dBalance) + " EUR"
是典型复制+粘贴错误。
与其复制自己,不如提取方法:
private static float ReadFloat(string title) {
// keep asking user...
while (true) {
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title);
if (!float.TryParse(Console.ReadLine(), out float result))
Console.WriteLine("Syntax error. Please, try again.");
else if (result < 0)
Console.WriteLine("Negative sum is not allowed. Please, try again.");
else
return result; // ... until valid value is provided
}
}
那就用吧
...
Console.WriteLine("Press 1 for WITHDRAWAL");
Console.WriteLine("Press 2 for DEPOSIT");
// Ask user once - Console.ReadKey().Key - then operate with his/her choice
var choice = Console.ReadKey().Key;
// balance change (either negative - withdraw or positive - deposit)
float delta =
choice == ConsoleKey.NumPad1 ? -ReadFloat("How much money do you wish to withdraw?")
: choice == ConsoleKey.NumPad2 ? ReadFloat("How much money do you wish to deposit?")
: 0f;
if (delta != 0) {
Console.WriteLine($"Your new balance is {startBalance + delta} EUR");
Console.ReadLine();
Environment.Exit(0);
}
else
Console.WriteLine("There was no choice for this number");
这是我的 ATM 代码,但不知何故,当我在小键盘上按数字 2 时,我得到了 else
声明,说没有这个号码的选择。
Console.WriteLine("What is your name? ");
string userName = Console.ReadLine();
Console.WriteLine("You are: " + userName);
Console.WriteLine("How much money do you have? ");
string Balance = Console.ReadLine();
float startBalance = float.Parse(Balance);
Console.WriteLine(userName + ", your balance is " + startBalance + " EUR");
Console.WriteLine("Press 1 for WITHDRAWAL");
Console.WriteLine("Press 2 for DEPOSIT");
if (Console.ReadKey().Key == ConsoleKey.NumPad1)
{
Console.ReadLine();
Console.WriteLine("How much money do you wish to withdraw? ");
string Withdrawal = Console.ReadLine();
float wBalance = float.Parse(Withdrawal);
Console.WriteLine("Your new balance is " + (startBalance - wBalance) + " EUR");
Console.ReadLine();
Environment.Exit(0);
}
if (Console.ReadKey().Key == ConsoleKey.NumPad2)
{
Console.ReadLine();
Console.WriteLine("How much money do you wish to deposit? ");
string Deposit = Console.ReadLine();
float dBalance = float.Parse(Deposit);
Console.WriteLine("Your new balance is " + (startBalance - dBalance) + " EUR");
Console.ReadLine();
Environment.Exit(0);
}
else Console.WriteLine("There was no choice for this number");
您有两个单独的 if
和两个单独的 Console.ReadKey()
.
相反,将该调用提取到一个变量中,并使用 if-else if-else 序列对其求值:
ConsoleKeyInfo.Key key = onsole.ReadKey().Key;
if (key == ConsoleKey.NumPad1)
{
Console.ReadLine();
Console.WriteLine("How much money do you wish to withdraw? ");
string Withdrawal = Console.ReadLine();
float wBalance = float.Parse(Withdrawal);
Console.WriteLine("Your new balance is " + (startBalance - wBalance) + " EUR");
Console.ReadLine();
Environment.Exit(0);
}
else if (key == ConsoleKey.NumPad2)
{
Console.ReadLine();
Console.WriteLine("How much money do you wish to deposit? ");
string Deposit = Console.ReadLine();
float dBalance = float.Parse(Deposit);
Console.WriteLine("Your new balance is " + (startBalance - dBalance) + " EUR");
Console.ReadLine();
Environment.Exit(0);
}
else
{
Console.WriteLine("There was no choice for this number");
}
复制+粘贴是邪恶:请注意减去(添加预期)存款
"Your new balance is " + (startBalance - dBalance) + " EUR"
是典型复制+粘贴错误。
与其复制自己,不如提取方法:
private static float ReadFloat(string title) {
// keep asking user...
while (true) {
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title);
if (!float.TryParse(Console.ReadLine(), out float result))
Console.WriteLine("Syntax error. Please, try again.");
else if (result < 0)
Console.WriteLine("Negative sum is not allowed. Please, try again.");
else
return result; // ... until valid value is provided
}
}
那就用吧
...
Console.WriteLine("Press 1 for WITHDRAWAL");
Console.WriteLine("Press 2 for DEPOSIT");
// Ask user once - Console.ReadKey().Key - then operate with his/her choice
var choice = Console.ReadKey().Key;
// balance change (either negative - withdraw or positive - deposit)
float delta =
choice == ConsoleKey.NumPad1 ? -ReadFloat("How much money do you wish to withdraw?")
: choice == ConsoleKey.NumPad2 ? ReadFloat("How much money do you wish to deposit?")
: 0f;
if (delta != 0) {
Console.WriteLine($"Your new balance is {startBalance + delta} EUR");
Console.ReadLine();
Environment.Exit(0);
}
else
Console.WriteLine("There was no choice for this number");