提问者:小点点

Switch语句未按预期工作


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

我想是因为switch语句,但我不知道具体在哪里。

但微软Visual Studio2015强调了一些事情。

例如:

(位于第24行)

(位于第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);
        }
    }
}

共3个答案

匿名用户

您从不初始化,而是将(codechoice/code>)与(code.startwith/code>)进行比较

还声明了两次

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

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;

匿名用户

的问题是,您已经在第14行用初始化了一个名为的变量。

要修复此问题,请将更改为

Input.StartsWith还返回一个布尔值,您正在将其与字符串进行比较。相反,使用等。

匿名用户

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;

        }