C# Switch语句未按预期工作

C# Switch语句未按预期工作,c#,switch-statement,C#,Switch Statement,我这里有个小问题,我需要一些帮助 我想这是因为switch语句,但我不知道确切的位置 但微软VisualStudio2015强调了一些事情 例如: string input=Console.ReadLine()(在第24行) input.StartsWith(“How”):(在第29行) 它说: 类型“bool”无法隐式转换为“string” 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading

我这里有个小问题,我需要一些帮助

我想这是因为switch语句,但我不知道确切的位置

但微软VisualStudio2015强调了一些事情

例如:

string input=Console.ReadLine()(在第24行)

input.StartsWith(“How”):
(在第29行)

它说:

类型“bool”无法隐式转换为“string”

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间Hello_World
{
班级计划
{
静态void Main(字符串[]参数)
{
string random=string.Empty;
字符串输入=string.Empty;
字符串选项=string.Empty;
//问点什么
做
{
Console.Clear();
WriteLine(“世界是一台野蛮的机器,无论你问什么,它都会给你一个肮脏的答案。”);
WriteLine(“这更像是一个笑话,如果你的问题没有得到回答,不要难过;D”);
控制台。写(“\nAsk World something:”);
字符串输入=Console.ReadLine();
}而(输入=“”);
开关(选择)
{
案例输入。与(“世卫组织”)一起启动:
WriteLine(“问题:谁在乎?”);
打破
案例输入。以(“方式”)开始:
Console.WriteLine(“Kys.就是这样。”);
打破
案例输入。开始使用(“其中”):
WriteLine(“这有什么意义?”);
打破
案例输入。使用(“如果”):
WriteLine(“如果有人关心,那会很有趣。”);
打破
案例输入。开始时(“何时”):
Console.WriteLine(“恐龙年轻时”);
打破
案例输入。开始时使用(“什么”):
Console.WriteLine(“这个问题的目的是什么?”);
打破
违约:
Console.Write(“现在不可用……我们正在处理它;D”);
打破
}
random=(“这是一个错误”);
//益母草
Console.Clear();
WriteLine(“对{0}的答案是:{1}”,输入,随机);
Console.ReadKey();
//比登
做
{
控制台。写入(“按退出…”);
while(Console.ReadKey().Key!=ConsoleKey.Enter){}
}虽然(正确);
}
}
}

您的问题是
字符串输入=Console.ReadLine()
是因为您已经在第14行用
string input=string.Empty初始化了一个名为
input
的变量

要解决此问题,请更改
string input=Console.ReadLine()
to
input=Console.ReadLine()

input.StartsWith还返回一个布尔值,您可以将其与字符串进行比较。相反,使用
案例“Who”:

以返回true/false开始,您将根据字符串检查该值

试试这个:

        switch (choice)
        {
            case "Who":
                Console.WriteLine("Question: Who cares?");
                break;

            case "How":
                Console.WriteLine("Kys. That's how.");
                break;
            ... OTHER CASES ...
            default:
                Console.Write("Not availible right now... we're working on it ;D");
                break;

        }

swtich
语句编码为嵌套的
if
语句。对于您的情况,您需要切换字符串的第一个单词。大概是这样的:

// only if at least two words in string!
var firstWordOfChoice = choice.Substring(0, choice.IndexOf(" ") - 1); 

switch (firstWordOfChoice) 
{
    case "Who":
        Console.WriteLine("Question: Who cares?");
        break;
    // and so on...
}
string choice = string.Empty;

// Ask something
do
{
    Console.Clear();
    Console.WriteLine("World is a Savage Machine, whatever you ask, it'll give you a nasty answer.");
    Console.WriteLine("This is more a Joke, don't be sad if your Question isn't answered ;D");
    Console.Write("\nAsk World something: ");
    input = Console.ReadLine();
} while (input == "");

choice = input.Split(' ')[0];

switch (choice)
{
    case "Who":
        Console.WriteLine("Question: Who cares?");
        break;
    case "How":
        Console.WriteLine("Kys. That's how.");
        break;

您从不初始化
choice
,而是将
string
choice
)与
bool
.StartWith
)进行比较

您还声明了两次
input

您可以拆分输入并将第一个单词放入
选项
,然后按如下方式打开:

// only if at least two words in string!
var firstWordOfChoice = choice.Substring(0, choice.IndexOf(" ") - 1); 

switch (firstWordOfChoice) 
{
    case "Who":
        Console.WriteLine("Question: Who cares?");
        break;
    // and so on...
}
string choice = string.Empty;

// Ask something
do
{
    Console.Clear();
    Console.WriteLine("World is a Savage Machine, whatever you ask, it'll give you a nasty answer.");
    Console.WriteLine("This is more a Joke, don't be sad if your Question isn't answered ;D");
    Console.Write("\nAsk World something: ");
    input = Console.ReadLine();
} while (input == "");

choice = input.Split(' ')[0];

switch (choice)
{
    case "Who":
        Console.WriteLine("Question: Who cares?");
        break;
    case "How":
        Console.WriteLine("Kys. That's how.");
        break;

case-input.StartsWith(“Who”)
返回一个bool,我猜,你的开关请求一个string这个代码没有任何意义;你说你想打开“选择”,但选择总是空的。顺便说一句,如果用户介绍了
Who
Who
Who
选项=input.split(“”[0]),你可以选择小写或大写,使其工作方式相同;似乎是一个错误修正到->choice=input.Split(“”)[0];