我怎样才能使这段代码正确地循环

本文关键字:代码 循环 正确地 段代码 | 更新日期: 2023-09-27 17:49:24

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的数字时,它确实会给我消息("那个数字太大了"),但问题是它仍然在代码末尾执行加法。我试着这样做,如果这些数字超出了这个范围,它会再次问我这些数字。我该怎么做,有什么建议吗?

我怎样才能使这段代码正确地循环

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

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();
}

您需要更好地控制外部循环。用while代替for,只有当你得到一个好的数字时才增加变量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而不是

你想太多了。如果你只是想把两个数相加,那就这么做。从逻辑上想一想。您可以根据需要添加错误消息。

  1. 获取a
  2. 的号码
  3. 获取b的号码
  4. 数字总和

注意:如果你需要两个以上的数字求和,那么这个解决方案将不起作用


//...
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();