小编Roz*_*Roz的帖子

如何使用C#中的方法存储变量?

我在下面的代码中遇到了一些问题.我还在学习,我不知道如何修复它.

1.我想要做的是创建一个方法(GetInt)来存储变量,就像我想要在第二个方法(GetTrack)中那样进入我的main方法.

2.-当无效输入时我无法获取GetInt方法循环,​​我猜测try/catch和boolean thingy有问题

谢谢

//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 …
Run Code Online (Sandbox Code Playgroud)

c# methods boolean try-catch do-while

-1
推荐指数
1
解决办法
85
查看次数

标签 统计

boolean ×1

c# ×1

do-while ×1

methods ×1

try-catch ×1