Switch语句未按预期工作

时间:2017-03-20 12:33:45

标签: c# switch-statement

我有一点问题,我需要一点帮助。

我认为这是因为switch语句,但我不确切知道在哪里。

但Microsoft Visual Studio 2015强调了一些内容。

如:

string input = Console.ReadLine(); (这是第24行)

input.StartsWith("How"): (在第29行)

它说:

  

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
            string random = string.Empty;
            string input = string.Empty;
            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: ");
                string input = Console.ReadLine();
            } while (input == "");

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

                case input.StartsWith("How"):
                    Console.WriteLine("Kys. That's how.");
                    break;

                case input.StartsWith("Where"):
                    Console.WriteLine("How does this make sense?");
                    break;

                case input.StartsWith("If"):
                    Console.WriteLine("If somebody would care, that'd be intresting.");
                    break;

                case input.StartsWith("When"):
                    Console.WriteLine("When the Dinasours were young");
                    break;

                case input.StartsWith("What"):
                    Console.WriteLine("What is the Purpose of this Question?");
                    break;

                default:
                    Console.Write("Not availible right now... we're working on it ;D");
                    break;

            }


            random = ("Das ist falsch.");

            // Antwort
            Console.Clear();
            Console.WriteLine("The Answer to {0} is: {1}", input, random);
            Console.ReadKey();


            // Beenden
            do
            {
                     Console.Write("Press <Enter> to exit... ");
                     while (Console.ReadKey().Key != ConsoleKey.Enter) { }
            } while (true);
        }
    }
}

4 个答案:

答案 0 :(得分:1)

您对string input = Console.ReadLine();的问题是因为您已经使用input在第14行初始化了名为string input = string.Empty;的变量。

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

input.StartsWith还返回一个布尔值,您将其与字符串进行比较。相反,使用 Case "Who":等。

答案 1 :(得分:0)

StartsWith返回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;

        }

答案 2 :(得分:0)

您编写的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...
}

答案 3 :(得分:0)

您永远不会初始化choice而您正在将stringchoice)与bool.StartWith)进行比较

你也宣布了input两次。

您可以拆分输入并将第一个单词转换为choice并按如下所示启用它:

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;