【问题标题】:Beginner C# User Input/Convert/Switch Statement Question初学者 C# 用户输入/转换/切换语句问题
【发布时间】:2021-02-28 09:29:48
【问题描述】:

我是初学者,刚开始用C#编写一个简单的计算器程序,当我在终端中运行我的应用程序时,按回车后它的用户输入仍然有ReadLine,如果我按两次回车,这些代码已发出

Unhandled exception. System.FormatException: Input string was not in a correct format.
   at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
   at System.Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)
   at System.Convert.ToInt32(String value)

这是我的代码

using System;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Enter Yor First Number");
      Console.ReadLine();
      int x = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine("Enter Yor Second Number");
      Console.ReadLine();
      int y = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine("Choose your calculation");
      Console.WriteLine("Type + for Addition");
      Console.WriteLine("Type - for Subtraction");
      Console.WriteLine("Type * for Multiplication");
      Console.WriteLine("Type / for Division");
      Console.ReadLine();
      string expression = Console.ReadLine();
      switch(expression) 
      {
        case "+":
          Console.WriteLine(x+y);
        break;
        case "-":
          Console.WriteLine(x-y);
        break;
        case "*":
          Console.WriteLine(x*y);
        break;
        case "/":
          Console.WriteLine(x/y);
        break;

      }
    }
  }
}

【问题讨论】:

    标签: c# switch-statement user-input calculator


    【解决方案1】:

    您必须检查用户输入,它是有效的 int 还是空的 如果它不是一个有效的数字,你必须让它再次输入数字

    using System;
    
    namespace MyApplication
    {
      public class Program
      {
        public void Main(string[] args)
        {
             FirstNumber:
          Console.WriteLine("Enter Yor First Number");
          string stringX = Console.ReadLine();
          int x = 0;
            if(String.IsNullOrWhiteSpace(stringX) || !int.TryParse(stringX, out x)){
                goto FirstNumber;
            }
            
             SecondNumber:
          Console.WriteLine("Enter Yor Second Number");
          string stringY = Console.ReadLine();
          int y = 0;
            if(String.IsNullOrWhiteSpace(stringY) || !int.TryParse(stringY, out y)){
                goto SecondNumber;
            }
            
             Expression:
          Console.WriteLine("Choose your calculation");
          Console.WriteLine("Type + for Addition");
          Console.WriteLine("Type - for Subtraction");
          Console.WriteLine("Type * for Multiplication");
          Console.WriteLine("Type / for Division");
    
          string expression = Console.ReadLine();
            
            if(expression!="+" && expression!="-" && expression!="/" && expression!="*"){
                goto Expression;
            }
          switch(expression) 
          {
            case "+":
              Console.WriteLine(x+y);
            break;
            case "-":
              Console.WriteLine(x-y);
            break;
            case "*":
              Console.WriteLine(x*y);
            break;
            case "/":
              Console.WriteLine(x/y);
            break;
    
          }
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      所以当你输入两次时,下一个ReadLine 方法被调用,它不是一个有效的整数值。因此,您应该始终检查该值是否可转换为整数。您可以使用int.TryParse 方法。您也可以使用 try catch 块进行调试。

      【讨论】:

      • @KennethCho 享受你的时光
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 2013-06-28
      • 2019-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      相关资源
      最近更新 更多