-5

这是我必须做的:

  1. 创建一个数组
  2. 用户输入 10 个双元素
  3. 使用 catch 确保它是有效数据
  4. 然后显示数据

这是到目前为止的代码:

static void Main(string[] args)
{
    double[] numbers;

    numbers = new double[10];

    for (int count = 0; count < 10; count += 1)
    {
        double num;

        Console.WriteLine("Enter a number");
        num = Convert.ToDouble(Console.ReadLine());

    }
    Console.WriteLine("{0}", numbers);
    Console.ReadLine();
}
4

3 回答 3

2
static void Main(string[] args)
{
    double[] numbers = new double[10];

    for (int count = 0; count < 10; count += 1)
    {
        double num;
        Console.WriteLine("Enter a number");

        while(!double.TryParse(Console.ReadLine(),out num))
        {
             Console.WriteLine("Not a valid number");
             Console.WriteLine("Enter a number");
        }
        numbers[count] = num;
    }

    foreach(double item in numbers)
    {
        Console.WriteLine("{0}", item);
    }

    Console.ReadLine();
}
于 2013-04-02T18:21:47.860 回答
0
static void Main(string[] args)
    {
        double[] numbers = new double[10];

        int counter = 0;


        do
        {
            Console.WriteLine("Enter valid number or press X to exit");
            var t = Console.ReadLine();
            try
            {
                double temp = double.Parse(t);

                numbers[counter] = temp;
                counter++;

            }
            catch (Exception)
            {
                if (t.Contains("X"))
                {
                    break;
                }
                //Console.WriteLine("Enter valid number or press X to exit");
            }


        } while (counter<10);

        foreach (var number in numbers)
        {
            Console.Write("\nEntered number: "+number);
        }

        Console.ReadLine();


    }
于 2013-04-02T18:57:14.377 回答
0
static void Main(string[] args)
{
    double[] numbers = new double[10];

    int i = 0;
    while (i < 10)
    {
        double num;

        Console.WriteLine("Enter a number");
        string input = Console.ReadLine();
        try
        {
            num = double.Parse(input);
            numbers[i] = num;
            i++
        }
        catch
        {
            Console.WriteLine("Invalid Number");
        }
    }

    foreach (double d in numbers)
        Console.WriteLine(d);
    Console.ReadLine();
}
于 2013-04-02T18:24:20.793 回答