【问题标题】:How to use two if statements in C#?如何在 C# 中使用两个 if 语句?
【发布时间】:2021-12-24 22:25:11
【问题描述】:

这是我的 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");

【问题讨论】:

  • 您是否使用调试器逐步完成此操作?
  • 当你有这个代码工作时,把它带到 codereview stackexchange - 这里有很多可以改进的地方,让它更干净。如果你现在养成好习惯,以后会有所帮助。
  • 希望我的银行在给我提款选项之前问我有多少钱! ????

标签: c# .net if-statement console


【解决方案1】:

您有两个独立的ifs 和两个独立的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");
}

【讨论】:

    【解决方案2】:

    复制+粘贴是邪恶的:请注意减法加法预期)存款

    "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");
     
    

    【讨论】:

      猜你喜欢
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2015-12-29
      • 2014-11-23
      • 2018-02-09
      相关资源
      最近更新 更多