【问题标题】:While loop and try and catchWhile 循环并尝试捕获
【发布时间】:2017-04-12 11:00:37
【问题描述】:

当输入不是数字时,代码会告诉我,但它也会同时编写 else if 代码。
我相信这与我在顶部双变量中将数字 0 分配给 doubleNr 有关,但如果我不这样做,我会得到 Use of unassigned local variable 'doubleNr' 在 while 条件下。
另外,我应该在哪里写 doubleNr = myMethod(intNr); 行?
在 try 块中还是在 catch 和 if 块之间?

int intNr;
double doubleNr = 0;

while (doubleNr < 20 || doubleNr > 30)
{
    Console.WriteLine("Enter your number: ");
    string strNr = Console.ReadLine();
    try
    {
        intNr = Convert.ToInt32(strNr);
        doubleNr = myMethod(intNr);  // Should this line go here?
    }
    catch
    {
        Console.WriteLine("Number must be INT");
    }
    // or should it go here?

    if (doubleNr < 20)
    {
        Console.WriteLine("Try a higher number.");
    }
    else if (doubleNr > 30)
    {
        Console.WriteLine("Try a lower number.");
    }
}
Console.WriteLine("That is a good number."); 

【问题讨论】:

  • 你不应该使用 try/catch 来控制你的程序的流程
  • doubleNr &lt; 20 || doubleNr &lt; 30 这没有意义,相当于说doubleNr &lt; 30 所以这里为什么要用两个条件呢?
  • 如果你不能确定输入是整数,不要使用Convert(),使用int.TryPase()。如果resul为假,这将给出布尔结果,所以“数字必须是INT”。
  • 是的@Tenakey 我不怪你。有些老师就是不知道如何进行不会导致坏习惯的try/catch练习......
  • 每次您想跳过当前循环执行时,只需使用continue;

标签: c#


【解决方案1】:

这可能不值得回答,但我只想指出您代码中的一些内容。由于您应该使用try-catch,您可以简单地实现retry-catch。为此,您应该只使用 while(true) 和您的 try-catch 逻辑来控制您的程序流程,如 @Pikoh 所说。

public static void Main(string[] args)
{
    int intNr;
    double doubleNr = 0;
    while(true)
    {
        try
        {
            Console.WriteLine("Enter your number: ");
            string strNr = Console.ReadLine();
            doubleNr = Int32.Parse(strNr);
            if(doubleNr < 20)
            {
                throw new Exception("Try a higher number.");
            }
            else if(doubleNr > 30)
            {
                throw new Exception("Try a lower number.");
            }
            else
            {
                Console.WriteLine("That is a good number.");
                break;
            }
        }
        catch(FormatException e)
        {
            Console.WriteLine("Enter a valid number.");
            continue;
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
            continue;
        }
    }
}

在这里您可以看到不是while 循环调节您的程序,try-catch 逻辑将控制它。当然还有其他方法可以做到这一点,但这只是对代码的重组。

请注意,还有更好的方法可以做到这一点。您可以使用Int32.TryParse(),这在这种情况下是最好的。无需使用任何 try-catch 块。

public static void Main(string[] args)
{
    int intNr;
    double doubleNr = 0;
    while(true)
    {
        Console.WriteLine("Enter your number: ");
        string strNr = Console.ReadLine();
        int value;
        if(Int32.TryParse(strNr, out value))
        {
            doubleNr = value;
        }
        else
        {
            Console.WriteLine("Enter a valid number: ");
            continue;
        }
        if(doubleNr < 20)
        {
            Console.WriteLine("Try a higher number.");
            continue;
        }
        else if(doubleNr > 30)
        {
            Console.WriteLine("Try a lower number.");
            continue;
        }
        else 
        {
            Console.WriteLine("That is a good number."); 
            break;
        }
    }
}

【讨论】:

    【解决方案2】:

    如果您想在出现异常或任何其他情况时跳过当前循环步骤,请使用continue 语句。

    continue 语句将控制权传递给 将它出现的 while、do、for 或 foreach 语句括起来。

    这是带有continue 语句的代码示例。

    int intNr;
    double doubleNr = 0;
    
    while (doubleNr < 20 || doubleNr > 30)
    {
        Console.WriteLine("Enter your number: ");
        string strNr = Console.ReadLine();
        try
        {
            intNr = Convert.ToInt32(strNr);
            // if you want to catch any exceptions in this method then leave it there 
            // otherwise you can move it outside try/catch block            
            doubleNr = myMethod(intNr);  
    
        }
        catch
        {
            Console.WriteLine("Number must be INT");
            continue; // return to the beginning of your loop
        }
    
        if (doubleNr < 20)
        {
            Console.WriteLine("Try a higher number.");
            continue; // return to the beginning of your loop
        }
        // do not need else since code wont reach this line if doubleNr is < 20
        if (doubleNr > 30) 
        {
            Console.WriteLine("Try a lower number.");                        
            continue; // return to the beginning of your loop
        }
    }
    Console.WriteLine("That is a good number."); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-13
      • 2013-12-15
      • 1970-01-01
      • 2019-10-29
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多