C#错误-停止菜单选择以中断编译器

C#错误-停止菜单选择以中断编译器,c#,validation,error-handling,C#,Validation,Error Handling,所以我制作了一个程序(c#),收集变量并对其进行计算 如何防止菜单选项导致错误?即,当尝试对空列表进行平均计算时,会中断程序 我应该使用list.length>1执行基本if语句吗? 还有谁能评论一下它的实用性等方面 using System; using System.Collections.Generic; using System.Linq; namespace exercise2._2 { public class Program { static Lis

所以我制作了一个程序(c#),收集变量并对其进行计算

如何防止菜单选项导致错误?即,当尝试对空列表进行平均计算时,会中断程序

我应该使用list.length>1执行基本if语句吗?
还有谁能评论一下它的实用性等方面

using System;
using System.Collections.Generic;
using System.Linq;
namespace exercise2._2
{
    public class Program
    {
        static List<int> list = new List<int>(); //Publicly accessable list declaration for integer collection
        
        static void Main(string[] args)
        {
                        Console.WriteLine("Hello and Welcome. \n Press Any Key To Enter the Menu "); //Output - Welcome and Menu display
                        Console.ReadKey();
                        menu();
        }

        public static void menu()
        {
            { start:                                                            //Label for validation and re-entering menu 
                Console.Clear();                                                //Keeps the menu in place and displays options
                Console.WriteLine("Please Select an Option:");
                Console.WriteLine("Press 1 To Enter Numerical Values ");
                Console.WriteLine("Press 2 To Peform Sum Calculation");
                Console.WriteLine("Press 3 To Perform Average Calculation");
                Console.WriteLine("Press 4 To Perform Median Calculation");
                Console.WriteLine(" ~~ Press 5 To Quit ~~  ");
                Console.Write("\r\nSelect an Option: ");

                string ans = Console.ReadLine();                                //Declaration for menu selection
                int ans1;

                if (!int.TryParse(ans, out ans1) )                              //Data Validation for menu selection (non numerical)
                {
                    Console.WriteLine( "~~Incorrect Data Input~~ \n Press Any Key To Continue" );
                    Console.ReadKey();
                    goto start;
                }

                if (ans1 == 1)                                                   //Option 1 of the menu
                {
                    list.Clear();                                               //Clear List Method
                    Collection();                                               //Populate List Method
                }

                 else if (ans1 == 2)                                            //Option 2 - Sum
                 {
                   double total = list.Sum();                                   //Variable declaration for the sum of the list elements
                   Console.WriteLine("The Sum of the numbers is " + total);     //Display for variable
                   Console.WriteLine("~~Press Any Key to return to the menu~~");
                   Console.ReadKey();
                   goto start;                                                  //Return to menu
                 }

                 else if (ans1 == 3)                                            //Option 3 - Average
                 {
                    double average = list.Average();                            //Variable declaration for the average of the list elements
                    Console.WriteLine("The Average is "+average);               //Display for variable
                    Console.WriteLine("~~Press Any Key to return to the menu~~");
                    Console.ReadKey();
                    goto start;                                                 //Return to menu
                 }

                 else if (ans1 == 4)                                            //Option 4 - Median
                 {
                    double median;                                              //Variable declaration
                    list.Sort();                                                //Sorting list numerically
                    int length = list.Count();                                  //Variable declaration for list length
                    int halfLength = list.Count()/2;                            //Variable declaration for half list length
                    
                    if (length % 2 == 0)                                        //if statement halfing the length and execute code depending on a remainder 
                    {
                        double med1 = list.ElementAt(halfLength) + list.ElementAt(halfLength - 1);//variable collecting and adding 2 middle values if list legnth is odd
                        median = med1 / 2;                                                        //Dividing by 2 for median
                    }                  
                    else
                    {
                        median = list.ElementAt(halfLength);                                  //Listing middle value if list length is even
                    }
                    Console.WriteLine("The Median is " + median);                             //Output median value
                    Console.WriteLine("~~Press Any Key To Return To The Menu~~");             
                    Console.ReadKey();
                    goto start;
                 }

                else if (ans1 == 5)                                            //Menu option 5 - Quit
                {
                    Console.WriteLine("~~Press Any Key To Exit~~");
                    Console.ReadKey();
                }
                else                                                             //Output if a number is entered that is not in the menu
                { 
                    Console.WriteLine(" --!! Please Make A Selection From The Menu !!-- \n Press Any Key To Continue ");
                    Console.ReadKey();
                    goto start;
                }
            }
        }

        static void Collection()                            //Method for collecting List elements
        { 
            string input;                                   //Variable declaration
            int inputV;
            int i;

            Console.WriteLine("Please Enter Values, if you wish to stop enter 0"); 
           
            

            for (i = 1; i != 0; i++)                                //For loop that runs until 0 is entered
            {
            retry:                                                  //start point for data validation
                Console.WriteLine("Please enter value " + i);       //Output to collect element and inform how many have been entered
                input = Console.ReadLine();

                if (!int.TryParse(input, out inputV))               //Data validation for list elements
                {
                    Console.WriteLine("Incorrect Data Input");      
                    goto retry;
                }
                if (inputV == 0)                                    //Instructing for loop what to do when 0 is entered
                {
                    menu();                                         //Method call for menu
                }
                list.Add(inputV); //add entries to the list         //For loop result. List population
                
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间练习2.\u 2
{
公共课程
{
static List List=new List();//整数集合的可公开访问的列表声明
静态void Main(字符串[]参数)
{
Console.WriteLine(“您好,欢迎。\n按任意键进入菜单”);//输出-欢迎和菜单显示
Console.ReadKey();
菜单();
}
公共静态无效菜单()
{
{开始://用于验证和重新进入菜单的标签
Console.Clear();//保留菜单并显示选项
Console.WriteLine(“请选择一个选项:”);
Console.WriteLine(“按1输入数值”);
Console.WriteLine(“按2键形成总和计算”);
Console.WriteLine(“按3进行平均计算”);
Console.WriteLine(“按4进行中值计算”);
Console.WriteLine(“~~按5退出~~”;
Console.Write(“\r\n选择一个选项:”);
字符串ans=Console.ReadLine();//菜单选择的声明
int ans1;
if(!int.TryParse(ans,out ans1))//菜单选择的数据验证(非数字)
{
Console.WriteLine(“~~数据输入错误~~\n按任意键继续”);
Console.ReadKey();
转到开始;
}
if(ans1==1)//菜单的选项1
{
list.Clear();//Clear list方法
Collection();//填充列表方法
}
else if(ans1==2)//选项2-求和
{
double total=list.Sum();//列表元素之和的变量声明
Console.WriteLine(“数字之和为“+total”);//显示变量
Console.WriteLine(“~~按任意键返回菜单~~”;
Console.ReadKey();
转到开始;//返回菜单
}
else if(ans1==3)//选项3-平均值
{
double average=list.average();//列表元素平均值的变量声明
Console.WriteLine(“平均值为”+平均值);//显示变量
Console.WriteLine(“~~按任意键返回菜单~~”;
Console.ReadKey();
转到开始;//返回菜单
}
else if(ans1==4)//选项4-中值
{
双中位数;//变量声明
list.Sort();//对列表进行数字排序
int length=list.Count();//列表长度的变量声明
int halfLength=list.Count()/2;//半列表长度的变量声明
if(长度%2==0)//if语句将长度减半,并根据余数执行代码
{
double med1=list.ElementAt(半长)+list.ElementAt(半长-1);//如果list legnth是奇数,则收集变量并添加两个中间值
中值=med1/2;//除以2表示中值
}                  
其他的
{
中值=list.ElementAt(half-length);//列出列表长度为偶数时的中间值
}
Console.WriteLine(“中间值为”+中间值);//输出中间值
Console.WriteLine(“~~按任意键返回菜单~~”;
Console.ReadKey();
转到开始;
}
else if(ans1==5)//菜单选项5-退出
{
Console.WriteLine(“~~按任意键退出~~”;
Console.ReadKey();
}
else//如果输入的数字不在菜单中,则输出
{ 
Console.WriteLine(“请从菜单中进行选择!!-->\n按任意键继续”);
Console.ReadKey();
转到开始;
}
}
}
斯达