循环练习?

时间:2015-01-02 02:34:05

标签: c# loops while-loop

        static void Main(string[] args)
        {
            int output = 0;
            int number = 0;
            Console.WriteLine("Please input a number for it to be counted!");
            bool conversion = int.TryParse(Console.ReadLine(), out output);
            if( number >= 1000)
            {
                while (number <= output)
                {
                    switch (conversion)
                    {
                        case true:
                            Console.Write(number + " ");
                            number += 2;
                            break;
                        case false:
                            Console.WriteLine("ERROR INVALID INPUT!");
                            break;

                    }
                }
            }
            else
            {
                Console.WriteLine("APPLICATION ERROR: NUMBER MUST BE BELOW OR AT 1000 TO PREVENT OVERFLOW!");
            }
            string choice = Console.ReadLine();
          do                                        // Here is the beginning of the do code
          {
              Console.WriteLine("Do you want to continue - Yes or No");
              if(choice.ToUpper() != "NO" && choice.ToUpper() != "NO");
              {
                  Console.WriteLine("ERROR INVALID INPUT: Only input Yes or No!");
              }
          }while(choice.ToUpper() != "YES" && choice.ToUpper() != "NO");
        }
    }
}

这是一个非常简单的应用程序并且尚未完成,但它的作用是你输入一个低于或等于1000的数字,它会均匀地计算它。我唯一的问题是代码末尾的do语句。它未完成但是从我所研究的那个语句完成后发生的事情是,如果用户没有输入是或否它会向他们显示该错误,然后再次询问&#34;你想再次去#34 ;然而,由于这个do语句,我输入任何数字,如10,它给我错误说它超过1000然后继续说无限循环&#34; ERROR INVALID INPUT:仅输入是或否!&#34;我该如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

你的if语句测试NOT EQUAL为“no”两次,我认为你的意思是检查不等于“YES”而不等于“NO”

答案 1 :(得分:0)

您在代码中遇到了几个问题:

首先,您的条件会检查number是否大于或等于1000.由于您输入的10不是>= 1000,那么您将进入{{1}声明。所以你需要将你的状况改为:

else

接下来,if(number <= 1000) 循环中的条件检查两次相同的条件...您不允许用户输入“是”。您应该修改条件以包括“是”的检查。此外,您需要从同一do语句的末尾删除分号:

if

您还可以考虑的一件事是添加一个方法来从用户那里获取一个整数,因为在一些验证中包装它是很方便的:

do
{
    Console.WriteLine("Do you want to continue - Yes or No");
    choice = Console.ReadLine(); 

    if(choice.ToUpper() != "NO" && choice.ToUpper() != "NO");
    {
        Console.WriteLine("ERROR INVALID INPUT: Only input Yes or No!");            
    }
} while(choice.ToUpper() != "YES" && choice.ToUpper() != "NO");

然后,当您需要从用户获取int时,可以调用此方法。以下是如何使用它的示例:

public static int GetIntFromUser(string prompt,
    string errorMessage = "Invalid entry. Please try again.")
{
    int value;

    while (true)
    {
        if (prompt != null) Console.Write(prompt);
        if (int.TryParse(Console.ReadLine(), out value)) break;
        if (errorMessage != null) Console.WriteLine(errorMessage);
    }

    return value;
}

答案 2 :(得分:0)

你的无限循环是因为条件检查choice的内容,但你永远不会在do循环中更改变量的值 - 只在循环开始之前的Console.ReadLine()中。

您应该在要重复的代码之前启动do循环。例如,如果你的意图是,如果用户输入yes表示整个过程重复,那么最好通过创建一个void函数来服务,比如static void CountNumbers() { ... },其中所有代码都在输入你的输入数量,然后你可以使用do循环轻松地重复用户想要的内容,如下所示:

string choice = null;
do
{
    CountNumbers();
    // Your code here to ask if user wants to continue and readline to get the new choice
} while ( /* Existing condition */);