公平警告,我是该语言的完整初学者。
我想使用C#进行简单的测验,该测验要求用户输入喜欢的颜色,然后根据它们输入的内容进行响应。现在,我只是在使用一堆IF语句来检查输入,但这要求我复制/粘贴这些if nif for每种颜色。有什么方法可以使用某种阵列,让我将所有颜色和响应放在一起?我知道您可以将数组用于数字,但是我无法弄清楚您是否可以对单词进行相同的操作。
再次,我是一个非常裸露的骨头的初学者,所以如果这是一个愚蠢的问题,我道歉。
对不起,这是我的代码:
using System;
namespace Whee
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is your favorite color?");
string color = Console.ReadLine();
if (color == "blue")
{
Console.WriteLine ("You must be a calm person.");
}
if else (color == "red")
{
Console.WriteLine ("You must be a Sith Lord.");
}
if (color == "green")
{
Console.WriteLine ("Dolla bills y'all!");
}
if (color == "orange")
{
Console.WriteLine ("What is this I don't even");
}
else
{
Console.WriteLine ("You fail at colors. Try again.");
}
}
}
替代通常建议的开关语句的替代方法是将您的值放在字典中。
然后,您可以将字典搜索输入值作为键,并返回该字典条目的值(如果存在(。否则,您可以输出"颜色失败。再试一次"。消息。
Dictionary<string, string> colors = new Dictionary<string, string>();
colors.Add("red", "You must be a Sith Lord.");
colors.Add("blue", "You must be a calm person.");
colors.Add("green", "Dolla bills y'all!");
colors.Add("orange", "What is this I don't even");
string color = Console.ReadLine();
string response = null;
colors.TryGetValue(color, out response);
Console.WriteLine(response ?? "You fail at colors. Try again.");
从长远来看,此方法可以更容易地维护,并且如果此应用程序复杂性增长,则不一定需要重新编译(Switch语句(。相反,可以将其从数据库,配置文件或任何适合这种情况的情况下提取。此数据源可以在运行时更新,而无需重新启动应用程序。
如注释中所述,您可以使用这样的'switch'语句:
static void Main(string[] args)
{
Console.WriteLine("What is your favorite color?");
string color = Console.ReadLine();
string response = "";
switch(color)
{
case "blue":
response = "You must be a calm person.";
break;
case "red":
response = "You must be a Sith Lord.";
break;
case "green":
response = "Dolla bills y'all!";
break;
case "orange":
response = "What is this I don't even";
break;
default:
response = "You fail at colors. Try again.";
break;
}
Console.WriteLine(response);
}
当您有一组if...then
时,只有1个一致的值是转置switch...case
结构"
string ColorResponse;
switch (color) {
case "blue": ColorResponse = "You must be a calm person."; break;
case "red": ColorResponse = "You must be a Sith Lord."; break;
case "green": ColorResponse = "Dolla bills y'all!"; break;
case "orange": ColorResponse = "What is this I don't even"; break;
// Added this, you can use multiple matches with the same output
case "fuschia":
case "chartous": ColorResponse = "Interesting shades."; break;
// You can add more logic in as well
case "caramel":
case "banana":
if (color == "caramel") { ColorResponse = "I like caramel on sundaes"; }
else { ColorResponse = "Banana splits are best with 2 cherries";
break;
// Always end with **default**
default: ColorResponse = "You fail at colors. Try again."; break;
}
Console.WriteLine(ColorResponse);
您还可以用do{}while()
的顺序使游戏循环结束。
例如:
do
{
Console.WritLine("What is your favorit color? (Enter QUIT to stop)");
String color = Console.ReadLine();
String output = "";
switch(color)
{
case "red":
output = "You must be a Sith Lord";
break;
case "QUIT":
output = "Good bye, thanks for playing!";
break;
}
Console.WriteLine(output);
}while(!Color.Equals("QUIT"))