【问题标题】:How to store a variable by using a method in C#?如何使用 C# 中的方法存储变量?
【发布时间】:2016-09-07 11:28:48
【问题描述】:

我对下面的代码有一些问题。我还在学习,不知道如何解决。

1 .-我要做的是创建一个方法 (GetInt) 来存储变量,就像我在第二个方法 (GetTrack) 中尝试做的那样,它将进入我的主要方法。

2.-当输入无效时,我无法让 GetInt 方法循环,​​我猜 try/catch 和布尔值有问题

谢谢

//Get int Method
static public void GetInt(string sPrompt, int iMin, int iMax)
{
    int iNum;
    bool bError = false;

    do
    {
        bError = true;
        try
        {
            Console.Write(sPrompt);
            iNum = int.Parse(Console.ReadLine());
            if ((iNum < iMin) || (iNum > iMax))
            {
                Console.WriteLine("The value is out of range.");
                bError = true;
            }
        }
        catch (ArgumentException)
        {
            Console.WriteLine("An invalid number was entered, please try again.");
            bError = true;
        }
    }
    while (bError == false);
}

//Get Track Method
static public void GetTrack()
{
    int iMin;
    int iSec;

    iMin = GetInt("Enter the minutes: ", 0, 10);
    iSec = GetInt("Enter the seconds: ", 0, 59);
}

【问题讨论】:

  • void 上进行快速 Google 搜索,以及 void 方法的作用......关于 return values
  • 你的try 只包装iNum = int.Parse(Console.ReadLine()); 可能会稍微好一点,这样你就不会捕捉(并忽略)其他异常。例如,如果sPromptnull,那么Console.Write 将抛出异常,但您的程序将简单地捕获该异常并说“无效的数字,再试一次”,即使数字是有效的,它是sPrompt 是无效的.

标签: c# methods boolean try-catch do-while


【解决方案1】:

这是我从控制台“获取”用户输入的方式:

            string choice = string.Empty;
            bool goodChoice = false;

            while (!goodChoice)
            {
                Console.Clear();
                Console.WriteLine(string.Empty);
                Console.WriteLine("Do you really want to hurt me?");
                Console.WriteLine(string.Empty);
                Console.WriteLine(string.Empty);
                Console.WriteLine("Please Y or N (or 0 to exit)");
                choice = Console.ReadLine().Trim();

                if (choice.Equals("Y", StringComparison.OrdinalIgnoreCase))
                {
                    goodChoice = true;
                }

                if (choice.Equals("N", StringComparison.OrdinalIgnoreCase))
                {
                    goodChoice = true;
                }

                if (choice.Equals("0"))
                {
                    goodChoice = true;
                    return; /* exist the routine */
                }
            }

根据您的情况修改

            string choice = string.Empty;
            bool goodChoice = false;

            while (!goodChoice)
            {
                Console.Clear();
                Console.WriteLine(string.Empty);
                Console.WriteLine("Enter an Integer between {0} and {1}", minValue, maxValue);
                Console.WriteLine(string.Empty);
                Console.WriteLine(string.Empty);
                Console.WriteLine("Please enter an integer (or X to exit)");
                choice = Console.ReadLine().Trim();

                int intParseResult = 0;
                bool intParseAttempt = int.TryParse(choice, out intParseResult);
                if(!intParseAttempt)
                {
                    goodChoice = false;
                }
                else
                {
                        if ((intParseResult < minValue) || (intParseResult > maxValue))
                        {
                                        Console.WriteLine("Out of Range");
                        }
                        else
                        {
                            goodChoice = true;
                        }
                }

                if (choice.Equals("X"))
                {
                    goodChoice = true;
                    return -99999; /* you'll have to figure out how to handle exits on your own */
                }
            }

【讨论】:

  • PS,请放弃匈牙利符号。它与Macarena一起消失了。 (例如:iNum,iMax)
【解决方案2】:

GetInt 方法的声明应更改为以下内容:

//Get int Method
static public int GetInt(string sPrompt, int iMin, int iMax)

从 do...while 循环的开头删除 bError = true; 语句。 在你的 do...while 循环之后,添加以下语句:

return iNum;

此外,您的while 条件应从bError == false 更改为bError == true 或简单地更改为bError,这意味着同样的事情,如果您的意图是继续提示用户直到输入被接受。

【讨论】:

    【解决方案3】:

    GetInt 的开头立即将bError 设置为true。这很可能应该是假的,所以你实际上会循环,因为你没有将它设置为假。

    此外,您不会从该方法返回任何东西,因此您不会得到任何东西。您必须更改方法以返回int,并在获取时实际返回值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2014-02-24
      • 2013-01-14
      相关资源
      最近更新 更多