【问题标题】:Unable to take input in correct format in c# [closed]无法在 C# 中以正确的格式输入 [关闭]
【发布时间】:2015-01-04 05:24:22
【问题描述】:

一个菜鸟,我无法使用以下代码正确地从控制台获取输入。

int id;
string name;
float duration;
float price;

Console.WriteLine("Enter the movie id");
id = Convert.ToInt16(Console.Read());

Console.WriteLine("Enter the movie name");
name = Console.ReadLine();

Console.WriteLine("Enter the movie duration");
duration = Convert.ToInt32(Console.Read());

Console.WriteLine("Enter the movie price");
price = Convert.ToInt32(Console.Read());

bl.addMovie(id, name, duration, price);

【问题讨论】:

  • 有什么问题或错误?
  • 您在 id 上使用 ToInt16 转换是否有任何特殊原因。
  • 奇怪的是,您在分配给浮点数的东西上使用 Convert.ToInt32,包括持续时间和价格。
  • 其实问题出在“name = Console.ReadLine();”行,要求输入电影名称和时长的行在不等待名称输入的情况下打印。
  • 不是真的,Nishant。你误解了那里发生的事情。请参阅 Jim 的回答。

标签: c# input console


【解决方案1】:

问题在于Console.Read 返回一个代表下一个字符输入的int。所以发生的情况是您输入多个字符并按 Enter 键,Read 返回一个字符。然后ReadLine 将其余字符作为字符串返回。

将您对Read 的调用替换为对ReadLine 的调用。

【讨论】:

    【解决方案2】:

    只读接受下一个字符。

    试试:

    float price;
    Console.WriteLine("Enter the movie price");
    string input = Console.ReadLine();
    int successStatus = float.TryParse(input, out price);
    bool success = successStatus != 0;
    

    或者简单地说:

    float price;
    Console.WriteLine("Enter the movie price");
    price = (float) Convert.ToDouble(Console.Readline())
    

    首选第一个,因为它在解析时更擅长处理错误。请参阅documentationfloat.TryParseSingle.TryParse 的同义词

    【讨论】:

    • “在解析时更好地处理错误”-但示例显示没有错误处理-因此在当前形式中,首先实际上更糟糕,因为它隐藏了无效输入。
    • TryParse 有内部处理。
    【解决方案3】:

    您需要做的是验证每个输入是否符合您的要求,这是一种方法

    static void Main(string[] args)
    {
        int id;
        string name;
        float duration;
        float price;
    
        do
        {
        Console.WriteLine("Enter the movie id");
        }
        while(!int.TryParse (Console.ReadLine(), out id));
    
        Console.WriteLine("Enter the movie name");
        name = Console.ReadLine();
    
        do
        {
            Console.WriteLine("Enter the movie duration");
        }
        while (!float.TryParse(Console.ReadLine(), out duration));
    
        do
        {
            Console.WriteLine("Enter the movie price");
        }
        while (!float.TryParse(Console.ReadLine(), out price));
    
        Console.WriteLine("{0}, {1}, {2}, {3}", id, name, duration, price);
    
        Console.ReadKey();
    }
    

    【讨论】:

      【解决方案4】:
      This worked fine.Use the Console.ReadLine.
      
        int id;
                  string name;
                  float duration;
                  float price;
      
                  Console.WriteLine("Enter the movie id");
                  id = Convert.ToInt16(Console.ReadLine());
      
                  Console.WriteLine("Enter the movie name");
                  name = Console.ReadLine();
      
                  Console.WriteLine("Enter the movie duration");
                  duration = Convert.ToInt32(Console.ReadLine());
      
                  Console.WriteLine("Enter the movie price");
                  price = Convert.ToInt32(Console.ReadLine());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-19
        相关资源
        最近更新 更多