【问题标题】:Asking for input thrice, what is wrong with my code?要求输入三次,我的代码有什么问题?
【发布时间】:2015-06-23 09:48:34
【问题描述】:
static void Main(string[] args)
{
    string name;
    int age;

    Console.WriteLine("How old are you?");

    string input = Console.ReadLine();
    if (int.TryParse(input, out age))
    {
        {
            agedetermine();
        }
    }
    else
    {
        Console.WriteLine("Give me an actual answer...");
        while (!int.TryParse(Console.ReadLine(), out age))
            Console.WriteLine("I don't have all day.");
        while (int.TryParse(Console.ReadLine(), out age))
        {
            agedetermine();
        }
    }

}

agedetermine() 只包含 ifConsole.WriteLine 不相关

对于第一个input = Console.ReadLine我特意输入了非整数来触发else,但是之后程序要输入3次整数回复。有人可以告诉我为什么并给我正确的编码吗? 我昨天刚开始所以我什么都不懂,所以请解释一下你在我的代码中引入的任何新术语的功能。

【问题讨论】:

    标签: c#


    【解决方案1】:

    您最后一个while (int.TryParse(Console.ReadLine(), out age)) 缺少!

    现在它会循环,直到您输入错误的内容,而不是相反。

    这应该是最后一个while(虽然没用):

    while (!int.TryParse(Console.ReadLine(), out age))
    

    防止代码重复的小建议:使用do...while

    do
    {
        Console.WriteLine("How old are you?");
    }
    while (!int.TryParse(Console.ReadLine(), out age));
    
    agedetermine();
    

    甚至用消息扩展:

    int age;
    
    string[] messages = new string[] { "How old are you?"
                                     , "Give me an actual answer..."
                                     , "I don't have all day."
                                     };
    int numberOfTries = 0;
    do
    {
        if (numberOfTries >= messages.Length)
        {
            Console.WriteLine(messages[messages.Length - 1]);
        }
        else
        {
            Console.WriteLine(messages[numberOfTries]);
        }
    
        numberOfTries++;
    }
    while (!int.TryParse(Console.ReadLine(), out age));
    
    agedetermine();
    

    【讨论】:

    • 是的,这非常有帮助,我很感激!我从您的回复中得到了更多问题._。 1. numberOfTries++中的加号是什么意思;做? 2. .Length 是什么意思?它是否涉及将字符串(当添加.Length)转换为整数? 3. string [] 是否意味着存储了超过 1 个数据?谢谢:)
    • 1.它是numberOfTries = numberOfTries + 1; 的缩写。 2.数组的长度。 3. 那就是数组的定义。通常你会输入一个数字来告诉数组的长度,但由于我立即实例化它,我不必这样做。
    【解决方案2】:

    最后一个 if 语句缺少 NOT 部分

    while (int.TryParse(Console.ReadLine(), out age))
    

    应该是

    while (!int.TryParse(Console.ReadLine(), out age))
    

    【讨论】:

      【解决方案3】:

      只需简化您的代码,您根本不需要最后一个 while 循环。

      static void Main(string[] args)
      {
          string name;
          int age;
      
          Console.WriteLine("How old are you?");
      
          string input = Console.ReadLine();
          {
              if (int.TryParse(input, out age))
              {
                  {
                      agedetermine();
                  }
              }
              else
              {
                  Console.WriteLine("Give me an actual answer...");
                  while (!int.TryParse(Console.ReadLine(), out age))
                      Console.WriteLine("I don't have all day.");
                  agedetermine();
      
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-28
        相关资源
        最近更新 更多