向数组中添加数字,c#只验证数字

本文关键字:数字 验证 添加 数组 | 更新日期: 2023-09-27 18:11:55

我这学期选修了一门c#课程,到目前为止还蛮有趣的。我有一个任务,我需要对数组做几件事:在数组中添加数字,查看在数组中输入的数字,在数组中找到一个数字,对数组中的数字进行排序,根据数组数字创建统计信息,最后退出应用程序。

到目前为止,我一直有一点问题与添加数字到数组,同时确保输入的数据只是数字。我想我马上就明白了,但帮助总是值得感激的。我的findData()方法还行吗?

再次感谢您花时间阅读这个问题!

class Program
{
    static char myChoice = Console.ReadKey().KeyChar;
    static double[] myArray = new double[100];
    static void Main(string[] args)
    {

        while (true)
        {
            Console.WriteLine("Welcome to Lab 2");
            Console.WriteLine();
            Console.WriteLine("Main Menu");
            Console.WriteLine("1- Add new data");
            Console.WriteLine("2- See all data");
            Console.WriteLine("3- Find a number");
            Console.WriteLine("4- Sort the data");
            Console.WriteLine("5- Create statistics");
            Console.WriteLine("6- Exit Program");

            switch (myChoice)
            {
                case '1':
                    Console.WriteLine("1- Add new data");
                    addData();
                    break;
                case '2':
                    Console.WriteLine("2- See all data");
                    seeData();
                    break;
                case '3':
                    Console.WriteLine("3- Find a number");
                    findData();
                    break;
                case '4':
                    Console.WriteLine("4- Sort the data");
                    sortData();
                    break;
                case '5':
                    Console.WriteLine("5- Create statistics");
                    createData();
                    break;
                case '6':
                    Console.WriteLine();
                    exitProgram();
                    break;
            }
        }
    }
    //This method will add numbers to the array
    public static void addData()
    {
        bool isNumber = false;
        double number;
        double temp;
        for (int i = 0; i < myArray.Length; i++)
        {
            Console.WriteLine("Enter a number you would like to add");
            myArray[i] = Convert.ToDouble(Console.ReadLine());
            temp = myArray[i];
            if (!Double.TryParse(temp, out number))
            {
                Console.WriteLine("Invalid input. Please enter a valid number")
            }
            else
            {
            }
        }
    }
    //This method will see the numbers entered in the array
    public static void seeData()
    {
        foreach (var item in myArray)
        {
            Console.WriteLine(item.ToString());
        }
    }
    //This method will find a specific number within the array and check if it has already been entered
    public static void findData()
    {
        Console.WriteLine("Find a number");
        string myChoice = Console.ReadLine();
        double number;
        bool isNumber = Double.TryParse(myChoice, out number);
        {
        }
    }
    //This method will sort the array ascending to descending
    public static void sortData()
    {
        Console.WriteLine("The array has been sorted in ascending order");
        Array.Sort(myArray);
        Console.WriteLine("The array has been sorted in descending order");
        Array.Reverse(myArray);
    }
    //This method will create statistics based on the numbers in the array
    public static void createData()
    {
        //Sum
        double sum = myArray.Sum();
        Console.WriteLine("The total sum of the array is: " + sum);
        //Average
        double average = sum / myArray.Length;
        Console.WriteLine("The average number of the array is: " + average);
        //Maximum
        double maximum = myArray.Max();
        Console.WriteLine("The maximum value in the array is: " + maximum);
        //Minimum
        double minimum = myArray.Min();
        Console.WriteLine("The minimum value in the array is: " + minimum);
        //Mean
        double mean = sum / myArray.Length;
        Console.WriteLine("The mean average of the array is: " + mean);
    }
    //This method will exit the program
    public static void exitProgram()
    {
        Environment.Exit(0);
    }
}

}

向数组中添加数字,c#只验证数字

我的findData()方法看起来好吗?

你的findData()方法实际上什么都不做。

这里有一个方法

public static void findData()
{
    Console.WriteLine("Find a number");
    string myChoice = Console.ReadLine();
    double number = -1;  
    if(!Double.TryParse(myChoice, out number))
    {
        Console.WriteLine("Invalid number");
    }
    else if (Array.IndexOf<double>(myArray, number) == -1)
    {
        Console.WriteLine("Number does not exist");
    }
    else
    {
        Console.WriteLine("Number does exist");
    }
}

这应该可以解决您的添加问题

  public static void addData()
            {
                for (int i = 0; i < myArray.Length; i++)
                {
                    bool success = false;
                    while (!success)
                    {
                        Console.WriteLine("Enter a number you would like to add");
                        string input = Console.ReadLine();
                        double number;
                        if (Double.TryParse(input, out number))
                        {
                            success = true;
                            myArray[i] = number;
                        }
                        else
                        {
                            Console.WriteLine("Invalid input. Please enter a valid number")
                        }
                    }
                }
            }

您的addData方法没有多大意义:您首先将双精度值插入到数组中,然后检查该值是否为双精度值(当然是,因为数组是双精度类型并且只能包含该类型的值)。

另外,如果用户输入无效,Convert.ToDouble可能会抛出异常。但是我看到您得到了使用Double.TryParse方法的要点,如果字符串(第一个参数)是有效数字,则返回true。所以你的addData方法应该是这样的:

//This method will add numbers to the array
public static void addData()
{
    double number;
    for (int i = 0; i < myArray.Length; i++)
    {
        Console.WriteLine("Enter a number you would like to add");
        // read user input
        string input = Console.ReadLine();
        // condition is true if user input is a number
        if (double.TryParse(input, out number))
            myArray[i] = number;
        else
            Console.WriteLine("Invalid input. Please enter a valid number");
    }
}

在你的数组中找到一个数字,你可以使用LINQ Contains扩展方法:如果数组中包含元素,它返回true,否则返回false:

//This method will find a specific number within the array and check if it has already been entered
public static void findData()
{
    double number;
    Console.WriteLine("Find a number");
    string input = Console.ReadLine();
    // we use the same logic here as in the addData method to make sure the user input is a number
    if (!double.TryParse(input, out number))
    {
        bool found = myArray.Contains(number);
        if (found)
            Console.WriteLine("Array has number {0}", number);
        else
            Console.WriteLine("Array doesn't have number {0}", number);
    }
    else
    {
        Console.WriteLine("Invalid input. Please enter a valid number");
    }
}