Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当一个方法从if-else语句返回一个值时,else语句中应该包含什么?_C#_If Statement_Methods - Fatal编程技术网

C# 当一个方法从if-else语句返回一个值时,else语句中应该包含什么?

C# 当一个方法从if-else语句返回一个值时,else语句中应该包含什么?,c#,if-statement,methods,C#,If Statement,Methods,我只是一个初学者,我有一个控制台应用程序,它可以计算一个正常工作的停车场的应付费用,至少在你输入一个正数之前是这样 我们刚刚开始使用多种方法,我的问题是,我传递“hours”值的方法是使用If-else语句来获取费用值,但当该方法返回一个double时,我对else的最佳实践有点困惑 理想情况下,我希望能够为“小时”输入一个无效值(负整数),程序将返回一条错误消息,并再次返回到程序的开始 为了达到这个效果,现在我在else上设置return,将传递回的值更改为0,然后在main方法中使用if语句

我只是一个初学者,我有一个控制台应用程序,它可以计算一个正常工作的停车场的应付费用,至少在你输入一个正数之前是这样

我们刚刚开始使用多种方法,我的问题是,我传递“hours”值的方法是使用If-else语句来获取费用值,但当该方法返回一个double时,我对else的最佳实践有点困惑

理想情况下,我希望能够为“小时”输入一个无效值(负整数),程序将返回一条错误消息,并再次返回到程序的开始

为了达到这个效果,现在我在else上设置return,将传递回的值更改为0,然后在main方法中使用if语句来处理“hours”=0的情况。然后我现在就用goto来做,但我不确定这是最佳实践。如果是的话,很好,但是如果有更好的方法,我宁愿不要依赖更混乱的东西

为所有的帮助干杯

    class Program
{        

    static void Main(string[] args)
    {
        double charge = 0;
        double hours = 0;
        string reg;

        start:

        while (hours != -999)
        {
            Console.Write("\nEnter hours : ");
            hours = Convert.ToDouble(Console.ReadLine());

            charge = CalcCharge(hours);     

            if (charge == 0)
            {
                Console.Write("Invalid hour value. Please try again.\n");
                goto start;
            }

            Console.Write("\nEnter reg : ");
            reg = Console.ReadLine();

            if (reg == "Sligo")
                charge = charge - ((charge / 100) * 10);

            if (charge > 100)
                charge = 100;

            Console.Write("\nThe charge is ${0:f2}.", charge);

            Console.ReadLine();
        }

    }

    static double CalcCharge(double hours)
    {
        double result;

        if (hours > 0 && hours < 7)
        {
            result = hours * 2;
            return result;
        }

        if (hours >= 7 && hours <= 10)
        {
            result = hours * 3;
            return result;

        }

        if (hours >= 11 && hours <= 15)
        {
            result = hours * 4;
            return result;

        }

        if (hours > 15)
        {
            result = hours * 3;
            return result;

        }

        else
        {
            return 0;
        }
    }
}
类程序
{        
静态void Main(字符串[]参数)
{
双电荷=0;
双倍小时=0;
字符串注册表;
开始:
而(小时数!=-999)
{
控制台。写入(“\n输入小时:”);
小时数=Convert.ToDouble(Console.ReadLine());
费用=计算费用(小时);
如果(费用=0)
{
Console.Write(“小时值无效。请重试。\n”);
转到开始;
}
控制台。写入(“\nEnter reg:”);
reg=Console.ReadLine();
如果(reg==“Sligo”)
电荷=电荷-((电荷/100)*10);
如果(充电>100)
电荷=100;
写入(“\n费用为${0:f2}.”,费用);
Console.ReadLine();
}
}
静态双充电(双小时)
{
双重结果;
如果(小时数>0和小时数<7)
{
结果=小时*2;
返回结果;
}
如果(小时数>=7和小时数=11和小时数15)
{
结果=小时*3;
返回结果;
}
其他的
{
返回0;
}
}
}

您可能不应该使用
else
,而应该抛出异常。此处使用的
ArgumentException
可能是正确的。捕获异常而不是检查返回值:

public static void Main(string[] args)
{
    //your code
    try
    {
        charge = CalcCharge(hours); 
    }
    catch(ArgumentException)
    {
        Console.Write("Invalid hour value. Please try again.\n");
        continue;
    }

...

static double CalcCharge(double hours)
{
    //Your code
    throw new ArgumentException("hours");
}

另外,避免使用
goto
,因为这是一种不好的做法,可能会导致非常混乱、粗俗的代码。如果你看这个例子,我使用了一个
continue
,它基本上是说“回到循环的开始”

我可能会将您的函数更改为:

static double CalcCharge(double hours)
{
    double result = 0;

    if (hours > 0 && hours < 7)
    {
        result = hours * 2;        
    }
    else if ((hours >= 7 && hours <= 10) || hours > 15)
    {
        result = hours * 3;
    }
    else if (hours >= 11 && hours <= 15)
    {
        result = hours * 4;
    }
    else
    {
         throw new ArgumentOutOfRangeException("there was a problem!");
    }
    return result;

}
静态双充电(双小时)
{
双结果=0;
如果(小时数>0和小时数<7)
{
结果=小时*2;
}
否则(小时数>=7小时和15小时)
{
结果=小时*3;
}

否则,如果当我尝试使用try/catch时(小时数>=11&&hours“在Sem2Assign1ExampleREAL.exe中发生类型为System.ArgumentOutOfRangeException”的未经处理的异常,则当我输入小时数的负值时,程序将挂起并抛出上述异常。抛出的新异常与您建议的完全相同,但try/catch是..try{charge=CalcCharge(hours);}catch(ArgumentOutOfRangeException){Console.Write(“小时值无效,请重试”);继续;}如果它运行了数小时,则条件:
hours!=-999
永远不会满足。您在
catch
中的消息是否打印出来?