C# 如何使此代码正确循环? inta=0; int b=0; 对于(int c=0;c=minValue&&h,你的开关的“断开”和循环的“断开”是令人困惑的。在案例1:a总是0如果数字小于或大于,为什么你有一个while来匹配?这两个while现在都没用了。while(条件){…break;}相当于if(condition){…}。这是正确的。我修复了它。很抱歉造成混淆。好的,我知道你在那里做了什么。c=1和c=2是什么意思?它们需要退出外部循环(第一个循环)。使用c=0进入循环,当c=2时退出,如果vara有效,则传递请求输入b并设置c=1当b有效时,设置c=2,外部循环终止。如果不是您想要的,则varc不会增加,并再次请求o插入正确的值非常感谢Steve.Loop-switch for flow control?Eeek。我正在尝试添加数字,但这是我的问题。如果数字不在范围内,我不希望它添加它。我希望系统告诉我使用范围内的数字重试。它当前正在做的是,如果我从范围内选择一个数字,它不会Let’我们告诉我这个数字太大了,但它仍然会在最后添加它。是的,您的原始代码中存在逻辑问题。对于一个“简单”的过程,您最初的尝试似乎很复杂。我添加了另一个示例,演示如何处理错误消息。非常感谢。您是一个救生员。 //... 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(); 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(); }

C# 如何使此代码正确循环? inta=0; int b=0; 对于(int c=0;c=minValue&&h,你的开关的“断开”和循环的“断开”是令人困惑的。在案例1:a总是0如果数字小于或大于,为什么你有一个while来匹配?这两个while现在都没用了。while(条件){…break;}相当于if(condition){…}。这是正确的。我修复了它。很抱歉造成混淆。好的,我知道你在那里做了什么。c=1和c=2是什么意思?它们需要退出外部循环(第一个循环)。使用c=0进入循环,当c=2时退出,如果vara有效,则传递请求输入b并设置c=1当b有效时,设置c=2,外部循环终止。如果不是您想要的,则varc不会增加,并再次请求o插入正确的值非常感谢Steve.Loop-switch for flow control?Eeek。我正在尝试添加数字,但这是我的问题。如果数字不在范围内,我不希望它添加它。我希望系统告诉我使用范围内的数字重试。它当前正在做的是,如果我从范围内选择一个数字,它不会Let’我们告诉我这个数字太大了,但它仍然会在最后添加它。是的,您的原始代码中存在逻辑问题。对于一个“简单”的过程,您最初的尝试似乎很复杂。我添加了另一个示例,演示如何处理错误消息。非常感谢。您是一个救生员。 //... 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(); 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(); },c#,C#,当我输入大于250或小于100的数字时,它确实会给我消息(“该数字太大”),但问题是它仍然在代码末尾执行加法。我正试图让它,如果这些数字不在这个范围内,它会再次要求我的数字。有什么建议吗?您需要更好地控制外部环路。而不是a用于一段时间,并且仅当您有一个好的数字时才递增变量C int a = 0; int b = 0; for (int c = 0; c < 2; c++) { Console.WriteLine("Give me a number"); int h =

当我输入大于250或小于100的数字时,它确实会给我消息(“该数字太大”),但问题是它仍然在代码末尾执行加法。我正试图让它,如果这些数字不在这个范围内,它会再次要求我的数字。有什么建议吗?

您需要更好地控制外部环路。而不是a用于一段时间,并且仅当您有一个好的数字时才递增变量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();
inta=0;
int b=0;
int c=0;
而(c<2)
{
Console.WriteLine(“给我一个号码”);
int-h;
if(!Int32.TryParse(Console.ReadLine(),out h)
{
Console.WriteLine(“不是有效数字”);
继续;
}
开关(c)
{
案例0:
a=h;
如果(a 250)
WriteLine(“该数字太大”);
其他的
c=1;
打破
案例1:
b=h;
如果(b<100 | | b>250)
WriteLine(“该数字太大”);
其他的
c=2;
打破
}
}
Console.WriteLine(“{0}”,a+b);
Console.ReadKey();
顺便说一下,我建议使用Int32.TryParse而不是Convert.ToInt32(如果用户键入无法转换为数字的内容,代码中会发生什么情况?)


我还修复了第二次测试中的一个输入错误。您应该使用变量b而不是a。您正在仔细考虑解决方案。如果您只是尝试对两个数字求和,那么只需这样做。逻辑思考。您可以根据需要添加错误消息

  • 找一个电话号码
  • 得到b的号码
  • 求和
  • 注意:如果您需要对两个以上的数字求和,那么此解决方案将不起作用


    /。。。
    int a=0;
    //捕获范围的值,并检查它
    而(a<10 | | a>50)
    {
    Console.WriteLine(“给我一个(a)的号码”);
    a=Convert.ToInt32(Console.ReadLine());
    }
    int b=0;
    //捕获b的值,并对其进行范围检查
    而(b<10 | | b>50)
    {
    Console.WriteLine(“给我一个(b)的号码”);
    b=Convert.ToInt32(Console.ReadLine());
    }
    Console.WriteLine(“{0}”,a+b);
    Console.ReadKey();
    //...
    

    编辑:

    inta=0;
    //获取一个值
    while(true)
    {
    Console.WriteLine(“给我一个(a)的号码”);
    a=Convert.ToInt32(Console.ReadLine());
    //范围检查并退出(如果有效)
    
    if(a>=10&&a=10&&b子例程非常棒,在许多情况下都很有用

    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();
    
    intgetnumberbetween(intminvalue,intmaxvalue)
    {
    int-h;
    对于(;;)
    {
    Console.WriteLine(“给我一个号码”);
    h=Convert.ToInt32(Console.ReadLine());
    
    如果(h>=minValue&&h,你的开关的“断开”和循环的“断开”是令人困惑的。在
    案例1:
    a
    总是0如果数字小于或大于,为什么你有一个
    while
    来匹配?这两个while现在都没用了。
    while(条件){…break;}
    相当于
    if(condition){…}
    。这是正确的。我修复了它。很抱歉造成混淆。好的,我知道你在那里做了什么。c=1和c=2是什么意思?它们需要退出外部循环(第一个循环)。使用c=0进入循环,当
    c=2
    时退出,如果var
    a
    有效,则传递请求输入
    b
    并设置
    c=1
    b
    有效时,设置
    c=2
    ,外部循环终止。如果不是您想要的,则var
    c
    不会增加,并再次请求o插入正确的值非常感谢Steve.Loop-switch for flow control?Eeek。我正在尝试添加数字,但这是我的问题。如果数字不在范围内,我不希望它添加它。我希望系统告诉我使用范围内的数字重试。它当前正在做的是,如果我从范围内选择一个数字,它不会Let’我们告诉我这个数字太大了,但它仍然会在最后添加它。是的,您的原始代码中存在逻辑问题。对于一个“简单”的过程,您最初的尝试似乎很复杂。我添加了另一个示例,演示如何处理错误消息。非常感谢。您是一个救生员。
    //...
    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();
    
    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();
    }