C#练习:输入两个数字,并显示他们。如果两个数字都大于10,要求用户重新输入。

2024-12-16 15:05:02
推荐回答(2个)
回答1:

goto少用 ,当你在以后的编程中是很少遇到这样的情况的 ,goto一般不要用,

你上面的代码还可以用死循环来做

                 static void Main(string[] args)

        {

            double var1, var2;

            bool bothofThemAreGreaterThan10;

            for(;;)
            {
              inputNumbers:


            Console.WriteLine("Please input a number:");


            var1 = Convert.ToDouble(Console.ReadLine());


            Console.WriteLine("Please input another number:");


            var2 = Convert.ToDouble(Console.ReadLine());


            bothofThemAreGreaterThan10 = (var1 > 10) & (var2 > 10);


            switch (bothofThemAreGreaterThan10)


            {


                case true:


                Console.WriteLine("Both of {0} and {1} are Greater than 10!Please input two numbers again:",var1,var2);break;




                case false:


                Console.WriteLine("You enterd {0} and {1}", var1, var2);


                break;


            }
            if(inputNumbers===0)
            {
//按1终止死循环 
            break;  
            } 
              } 
            Console.ReadKey();
}

回答2:

尽量不要用goto语句,可以用以下代码:
int one, two;
for (; ; )
{
Console.WriteLine("请输入one的值");
one = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入two的值");
two = Convert.ToInt32(Console.ReadLine());
if (one > 10 && two > 10)
Console.WriteLine("值都大于10,请重新输入");
else
{
Console.WriteLine("one={0},two={1}", one, two);
break;
}
}
Console.Read();