CS 1061例外 [英] CS 1061 exception

查看:71
本文介绍了CS 1061例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试获取菜单选择项的选择号时遇到了异常.
这是代码,任何人都可以帮助我.

I am running into an exception when I try to get the choice number for my menu choice items.
here is the code can anyone help me.

&public public int  getMenuChoice()
       {
           Console.WriteLine(选择一个选项号.\ n1.创建字符\ n2.修改字符\ n3.设备
          武器\ n4.创建一个新武器\ n5.状态\ n6. END");
          字符串选择= Console.ReadLine();

           //声明变量以保持转换后的值
           int numChoice;

           //开始循环验证
           while(!int.TryParse(choice,out numChoice))
           {   //开始循环

                             //告诉用户问题                              Console.WriteLine(您输入的是数字以外的其他数字.\ r \ n请输入一个数字");

                             //捕获新用户的响应,并返回到循环的开头
                            选择= Console.ReadLine();  //循环返回
           }
       } 

 public static int  getMenuChoice()
        {
            Console.WriteLine("CHOOSE AN OPTION NUMBER. \n1. CREATE CHARACTER\n2. MODIFY CHARACTER\n3. EQUIP
            WEAPON\n4. CREATE A NEW WEAPON \n5. STATUS\n6. END");
            string choice = Console.ReadLine();

            //Declare the variable to hold the converted value
            int numChoice;

            //Start the loop validation
            while (!int.TryParse(choice, out numChoice))
            {   //begin loop

                //tell the user the problem
                Console.WriteLine("You have typed something other than a number.\r\nPlease enter a number.");

                //Catch the new user response and go back to beginning of the loop
                choice = Console.ReadLine();  //loop back
            }
        } 

推荐答案

i 欢迎访问MSDN论坛.

我试图将您的代码复制到VS中并进行构建,发现了错误消息您提到的如下:

>>

>>

CS0161

'Program.getMenuChoice()': 并非所有代码路径都返回值

由于您的方法"getMenuChoice"被定义为"int"类型,而您没有返回一个'int'值 在此方法的末尾,因此会弹出编译器错误代码'CS0161' .您可以尝试在如下所示的方法中添加返回值,然后此编译器错误将 消失.

Since your method "getMenuChoice" is defined as the ‘int’ type and you did not return a ‘int’ value at the end of this method, so it popups the complier error code ‘CS0161’. You can try to add the return value in the method like the following, then this compiler error will disappear.

public 静态 int

        public static int getMenuChoice()

{

        {

;   控制台 选择 一个选项号. \ n1.创建字符\ n2.修改字符\ n3.装备武器\ n4.创建新武器\ n5.STATUS \ n6.END"; ) ;

            Console.WriteLine("CHOOSE AN OPTION NUMBER. \n1. CREATE CHARACTER\n2. MODIFY CHARACTER\n3. EQUIP WEAPON\n4.CREATE A NEW WEAPON \n5.STATUS\n6.END");

;   字符串 控制台

            string choice = Console.ReadLine();

;   //声明变量以保存转换后的值

            //Declare the variable to hold the converted value

;   int

            int numChoice;

;   //开始循环验证

            //Start the loop validation

;   int .TryParse(选择, out

            while(!int.TryParse(choice, out numChoice))

;   {   //开始循环

            {   //begin loop

;      //向用户说明问题

                //tell the user the problem

;      控制台 您 \ r \ n请输入数字."

                Console.WriteLine("You have typed something other than a number.\r\nPlease enter a number.");

;      //捕获新的用户响应并返回到循环的开始

                //Catch the new user response and go back to beginning of the loop

;      choice = 控制台 //返回

                choice = Console.ReadLine();  //loop back

;   }

            }

;   numChoice = 转换

            numChoice = Convert.ToInt32(choice);

;   返回

            return numChoice;

}

        }

最好的问候,

Sara


这篇关于CS 1061例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆