1
 class AddItemOption
{
    //Fields that contain class data
    string input = Console.ReadLine(); 
   //Methods that define class functionality
    public string myFunction(string Value)
    {
        switch (input)
        {
            case "Umbrella":
               Console.WriteLine("Umbrella is selected");
               break;
            case "Rain Coat":
               Console.WriteLine("Raincoat is selected");
               break;
            case "Boots":
                Console.WriteLine("Boots is selected"); 
                break;
            case "Hood":
                Console.WriteLine("Hood is selected");
                break;
            default:
                Console.WriteLine("Input not reconized, please choose another item");
                break;


        }

    }

我收到错误“并非所有代码路径都返回值”。它来自 myFunction(string Value)。我不确定如何返回它,或者在参数中放入什么以使其工作。我也需要它下面的东西吗?我是新来的课程。请告诉我我是否做错了,或者这是否是 switch 语句应该去的地方!

   public AddItemOption(string input) 

        {


        }

从 Shyju 我把它改成了:

class AddItemOptionOne
{
    //Fields that contain class data
    string input = Console.ReadLine(); 
   //Methods that define class functionality
    public string myFunction(string Value)
    {
        switch (input)
        {
            case "Key Chain":
               return "Key Chain is selected";
               break;
            case "Leather Armor":
               return "Leather Armor is selected";
               break;
            case "Boots":
                return "Boots is selected"; 
                break;
            case "Sword":
                return "Sword is selected";
                break;
            default:
                return "Input not reconized, please choose another item";
                break;


        }
    }

但是,它不识别中断。“检测到无法访问的代码”

4

3 回答 3

5

您的方法的返回类型为string. 这意味着你的方法应该总是返回一些字符串。但是你没有返回一个字符串。相反,您正在使用方法将其写入控制台WriteLine

所以改变

Console.WriteLine("Umbrella is selected");

return "Umbrella is selected";

或者

您可以将方法的返回类型更改为void.

 public void myFunction(string Value)
 {
   //your current fancy stuff here
   Console.WriteLine("My method dont have a return type");
 }

编辑:根据问题编辑。

return语句将控制您的方法。这意味着下一行 ( break) 不会被执行。所以删除break语句。

 case "Key Chain":
           return "Key Chain is selected";
于 2012-11-06T01:34:43.840 回答
2

嗯......你不会从你的函数中返回任何东西,所以把它改成

public void myFunction(string Value)
于 2012-11-06T01:36:56.693 回答
2

尝试这个:

class AddItemOptionOne
{
    //Methods that define class functionality
    public string myFunction(string input)
    {
        string result = string.Empty;

        switch (input)
        {
            case "Key Chain":
                result = "Key Chain is selected";
                break;
            case "Leather Armor":
                result = "Leather Armor is selected";
                break;
            case "Boots":
                result = "Boots is selected";
                break;
            case "Sword":
                result = "Sword is selected";
                break;
            default:
                result = "Input not reconized, please choose another item";
                break;
        }

        return result;
    }
}
于 2012-11-06T01:46:43.540 回答