如何让这段代码正确循环?

时间:2014-03-09 23:17:36

标签: c#

int a = 0;
int b = 0;

for (int c = 0; c < 2; c++)
{
    Console.WriteLine("Give me a number");

    int h = Convert.ToInt16(Console.ReadLine());
    switch (c)
    {
        case 0:
            a = h;
            while (a <100 || a>250)
            {
                Console.WriteLine("That number is too large");
                break;
            }

            break;

        case 1:
            b = h;
            while (a < 100 || a > 250)
            {
                Console.WriteLine("That number is too large");
                break;
            }

            break;
    }
}

Console.WriteLine("{0}",a+b);
Console.ReadKey();

当我输入大于250或小于100的数字时,它会给我一条消息(“那个数字太大”),但问题是它仍然在代码末尾执行添加。我试图做到这一点,如果这些数字超出该范围,它再次问我数字。关于如何做到这一点的任何提示?

3 个答案:

答案 0 :(得分:5)

子程序是很棒的东西,在很多情况下很有用。

int GetNumberBetween( int minValue, int maxValue )
{
    int h;
    for (;;)
    {
        Console.WriteLine("Give me a number");
        h = Convert.ToInt32(Console.ReadLine());
        if ( h >= minValue && h <= maxValue )
            break;
        Console.WriteLine("I don't like that number, try again");
    }
    return( h );
}

void DisplaySum( void )
{
    int a = GetNumberBetween( 100, 250 );
    int b = GetNumberBetween( 100, 250 );
    Console.WriteLine("{0}",a+b);
    Console.ReadKey();
}

答案 1 :(得分:2)

您需要更好地控制外部循环。而不是使用一段时间,只有当你有一个好的数字时才增加变量C.

int a = 0;
int b = 0;
int c = 0;
while (c < 2)
{
    Console.WriteLine("Give me a number");
    int h;
    if(!Int32.TryParse(Console.ReadLine(), out h)
    {
        Console.WriteLine("Not a valid number");
        continue;
    }
    switch (c)
    {
        case 0:
            a = h;
            if(a <100 || a>250)
                Console.WriteLine("That number is too large");
            else 
               c = 1;
            break;

        case 1:
            b = h;
            if(b < 100 || b > 250)
                 Console.WriteLine("That number is too large");
            else
               c = 2;
            break;
    }
}

Console.WriteLine("{0}",a+b);
Console.ReadKey();

顺便说一句,我建议使用Int32.TryParse而不是Convert.ToInt32(如果用户键入了无法转换为数字的内容,代码中会发生什么?)

我还在你的第二次测试中修正了一个拼写错误。您应该使用变量b而不是

答案 2 :(得分:1)

你在想解决方案。如果你只是试图将两个数字相加,那么就这样做吧。从逻辑上考虑它。您可以根据需要添加错误消息。

  1. 获取
  2. 的号码
  3. 获取b
  4. 的编号
  5. 数字总和
  6. 注意:如果您需要汇总两个以上的数字,那么此解决方案将无效


    //...
    int a = 0;
    
    //Capture a value for a, and range check it
    while (a < 10 || a > 50)
    {
        Console.WriteLine("Give me a number for (a)");
        a = Convert.ToInt32(Console.ReadLine());
    }
    
    int b = 0;
    
    //Capture a value for b, and range check it
    while (b < 10 || b > 50)
    {
        Console.WriteLine("Give me a number for (b)");
        b = Convert.ToInt32(Console.ReadLine());
    }
    
    Console.WriteLine("{0}", a + b);
    Console.ReadKey();
    //...
    

    编辑:

    int a = 0;
    
    //Get a value for a
    while (true)
    {
        Console.WriteLine("Give me a number for (a)");
        a = Convert.ToInt32(Console.ReadLine());
    
        //Range check and exit if valid
        if (a >= 10 && a <= 50)
            break;
    
        Console.WriteLine("That number is too large");
    }
    
    int b = 0;
    
    //Get a value for b
    while (true)
    {
        Console.WriteLine("Give me a number for (b)");
        b = Convert.ToInt32(Console.ReadLine());
    
        //Range check and exit if valid
        if (b >= 10 && b <= 50)
            break;
    
        Console.WriteLine("That number is too large");
    }
    
    Console.WriteLine("{0}", a + b);
    Console.ReadKey();