我这里有个小问题,我需要一点帮助。
我想是因为switch语句,但我不知道具体在哪里。
但微软Visual Studio2015强调了一些事情。
例如:
上面说:
类型“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);
}
}
}
您从不初始化
还声明了两次
您可以拆分输入并将第一个单词放到
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;
要修复此问题,请将
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;
}