如何让这个 C# 程序一直循环直到用户输入一个整数?

问题描述 投票:0回答:1

我希望程序不断要求用户输入数字,直到他们最终使用正确的数据类型,而不是仅仅退出程序。这是我写的。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test1
{
    class Program
    {
        static void Main(string[] args)
        {
            //declare and initialize variables
            int a = 0;
            int b = 0;
            int area = 0;
            string read;
            
            do { 
                try
                {
                    //input data solicited
                    Console.WriteLine("Rectangle height: ");
                    read = Console.ReadLine();
                    a = int.Parse(line);

                    Console.WriteLine("Rectangle width: ");
                    read = Console.ReadLine();
                    b = int.Parse(line);

                    //operation
                    area = a * b;

                    //print result
                    Console.WriteLine("The area of the rectangle is: " + area + " cm²");
                    
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR. Wrong data type.");
                }
            }while (e);
        }
    }
} 

我尝试使用布尔值作为随机猜测,使 do-while 循环继续运行,直到用户成功输入一个整数,但这也不起作用。你在底部看到的

while (e);
也是一个疯狂的猜测。

                    Console.WriteLine("The area of the rectangle is: " + area + " cm²");
                    cont = false; 
                    
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR. Ingresa un numero entero.");
                    cont = false; 
                }
            }while (cont);

我不能为此使用 if 语句。

c# loops exception visual-studio-2022 do-while
1个回答
0
投票

有两种方法可以让它循环直到数字。

一种方法是创建一个以 false 开头的 bool,然后循环直到 bool 为 true,所以如果成功,则不再继续循环。

另一种方法是创建一个

do {} while (true)
,但
break
位于
try
堆栈的末尾。

由于代码到达最后(

break
isValid = true
),因此需要代码才能正常工作。

如果您愿意,可以将 try {} 替换为变量类型检查,如果是数字,则继续,如果有其他情况,则继续循环(带否定条件)。

使用变量

...
static void Main(string[] args)
{
    // declare and initialize variables
    int a = 0;
    int b = 0;
    int area = 0;
    string read;
    bool isValid = false;

    while (!isValid) { 
        try
        {
            // Input data solicited
            Console.WriteLine("Rectangle height: ");
            read = Console.ReadLine();
            a = int.Parse(read);

            Console.WriteLine("Rectangle width: ");
            read = Console.ReadLine();
            b = int.Parse(read);

            //operation
            area = a * b;

            //print result
            Console.WriteLine("The area of the rectangle is: " + area + " cm²");
            isValid = true;
            
        }
        catch (Exception e)
        {
            Console.WriteLine("ERROR. Wrong data type, expected number.");
        }
    };
}
...

使用中断

...
static void Main(string[] args)
{
    // declare and initialize variables
    int a = 0;
    int b = 0;
    int area = 0;
    string read;

    do { 
        try
        {
            ...
            break
            
        }
        catch (Exception e)
        {
            ...
        }
    } while (true);
}
...

另外,

a = int.Parse(line)
中有一个错误,没有行var。与变量 b 相同。

© www.soinside.com 2019 - 2024. All rights reserved.