【问题标题】:Experiencing an issue with my while loop我的 while 循环遇到问题
【发布时间】:2012-11-04 15:46:57
【问题描述】:

我想要一些关于我在哪里出错的建议。控制台应用程序显示菜单选项并要求用户输入有效的菜单选项。

现在它的作用是,如果输入的数字是 1、2 或 3,它会显示“您已选择选项 x”然后“按任意键关闭”但程序不会显示“您已选择选项 x' 然后'按任意键关闭'。如果数字小于 1 或大于 3,它会起作用,说“菜单选择不在 1-3 之间”然后“请重新输入”。我哪里错了?

我已经很久没有编程了,如果我这次能纠正我的错误,我知道未来。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _4._6
{
    class Program
{
    static void Main(string[] args)
    {
        int iChoice = 0;

        Console.WriteLine("1: Add two numbers");
        Console.WriteLine("2: Multiply two numbers");
        Console.WriteLine("3: Exit the program");

        Console.WriteLine("Enter your choice: ");
        iChoice = Convert.ToInt32(Console.ReadLine());


        while (iChoice < 1 || iChoice > 3)
        {
            Console.WriteLine("Menu choice not between 1-3: ");
            Console.Write("Please re-enter: ");
            iChoice = Convert.ToInt32(Console.ReadLine());
            Console.ReadKey();

            if  (iChoice > 1 || iChoice < 3)
            {
                Console.WriteLine("You have chosen option " + iChoice);
                iChoice = Convert.ToInt32(Console.ReadLine());
                Console.ReadKey();

            }



        }
    }
}

}

【问题讨论】:

  • 你能做的最好的事情就是学会使用调试器。然后你可以单步调试代码,看看发生了什么。
  • 只有当 iChoice 不是 [1..3] 时才执行的 while 循环中的代码?在这种情况下,iChoice 将永远不会处于您的 if 语句指定的条件中。

标签: c#


【解决方案1】:

你的内部 if 永远无法输入,因为它与外部 if 的条件相矛盾:

这样改

    while (iChoice < 1 || iChoice > 3)
    {
        Console.WriteLine("Menu choice not between 1-3: ");
        Console.Write("Please re-enter: ");
        iChoice = Convert.ToInt32(Console.ReadLine());
        Console.ReadKey();

    }


    Console.WriteLine("You have chosen option " + iChoice);

还要注意代码中第二个 if 的条件是错误的

【讨论】:

  • 运行此代码后,行“iChoice = Convert.ToInt32(Console.ReadLine());”突出显示,表示未处理 FormatException - 输入字符串的格式不正确。
【解决方案2】:

if 块应该在 while 块之外。因为如果条件为假,您的 while 块将不会运行您的 if 块。这意味着你不允许你的 if 块检查它的条件

【讨论】:

    【解决方案3】:

    您在代码中的两个位置分配给iChoice。也许说起来不那么令人困惑:

        Console.WriteLine("1: Add two numbers");
        Console.WriteLine("2: Multiply two numbers");
        Console.WriteLine("3: Exit the program");
    
        Console.WriteLine("Enter your choice:");
    
        int iChoice;
        while (true)
        {
          int.TryParse(Console.ReadLine(), out iChoice);
          if (iChoice >= 1 && iChoice <=3)
            break; // choice is OK
    
          Console.WriteLine("Menu choice not between 1-3.");
          Console.Write("Please re-enter:");
        }
    
        Console.WriteLine("You have chosen option " + iChoice);
        // ...
    

    注意:我刚刚将Convert.ToInt32 更改为int.TryParse。好处是如果用户输入的不是数字,比如“sdfih”,它只会使iChoice 为零,而不是让应用程序崩溃。

    【讨论】:

    • TryParse 返回一个布尔值,指示解析是否成功。不要检查 iChoice 是否为零,而是检查 TryParse 是否返回 true。
    • @BAF 是的,在一般情况下,可以说类似if (parsed &amp;&amp; iChoice &gt;= 1 &amp;&amp; iChoice &lt;=3),其中parsed 是int.TryParse 的布尔返回值,但在这种情况下,因为0 不是无论如何,一个合法的值,一个可能是草率的,只需检查out 参数。只要尝试解析失败(返回false),out 参数就保证为0。
    猜你喜欢
    • 1970-01-01
    • 2017-06-02
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2011-06-19
    相关资源
    最近更新 更多