C# 如何执行类似if(在if==“x”之前键入key)的操作

C# 如何执行类似if(在if==“x”之前键入key)的操作,c#,C#,好的,我是c#的一个noob,我想做一个简单的测试 鉴于我还不知道要做一个界面测试,我尝试了一个非常简单的,类似这样的测试 Console.WriteLine("question"); Console.WriteLine("a" + " " + " " + " " + " " + "b" + " " + " " + " " + " " + "c"); Console.ReadLine(); if (Console.ReadLine() == "a") {

好的,我是c#的一个noob,我想做一个简单的测试 鉴于我还不知道要做一个界面测试,我尝试了一个非常简单的,类似这样的测试

Console.WriteLine("question");
Console.WriteLine("a" + " " + " " + " " + " " + "b" + " " + " " + " " + " " + "c");
Console.ReadLine();
 if (Console.ReadLine() == "a")
            {
                Console.WriteLine("comment");
            }
 else if (Console.ReadLine() == "b")
            {
                Console.WriteLine("comment");
            }
 else if (Console.ReadLine() == "c")
            {
                Console.WriteLine("comment");
            }
string str = Console.ReadLine();
switch(str)
{
case "a":
    Console.WriteLine("comment");
    break;
case "b":
    Console.WriteLine("comment");
    break;
case "c":
    Console.WriteLine("comment");
    break;
}
我知道这很可怕,但在我目前的水平上,我看不到任何其他的测试方法 直到我知道之前,我不明白为什么它让我把答案打3次,然后我意识到读线就是这么做的
我可以用这种简单的方法做一些事情吗?或者我需要提高编程技能吗?

将结果保存在字符串变量中并引用它:

string choice = Console.ReadLine();
if (choice == "a")
{
    Console.WriteLine("comment");
}
else if (choice == "b")
{
    Console.WriteLine("comment");
}
使用哪个更整洁 并将
Console.ReadLine()
的结果保存到

多个
Console.ReadLine()
将导致多个询问答案

Console.WriteLine(“问题”);
字符串userChoice=Console.ReadLine();
开关(用户选择)
{
案例“a”:
WriteLine(“您的选择是一个”);
打破
案例“b”:
Console.WriteLine(“您的选择是b”);
打破
案例“c”:
Console.WriteLine(“您的选择是c”);
打破
}

你错过了这个简单的事实 当程序到达Console.ReadLine()时,它将从Console.ReadLine()读取数据 你能做的就是这样

string str=Console.ReadLine();
如果(str==“a”)
{
Console.WriteLine(“评论”);
}
否则如果(str==“b”)
{
Console.WriteLine(“评论”);
}
否则如果(str==“c”)
{
Console.WriteLine(“评论”);
}
您知道,只要检查语句的右边是常量,最好使用switch case语句
那么你的代码应该是这样的

Console.WriteLine("question");
Console.WriteLine("a" + " " + " " + " " + " " + "b" + " " + " " + " " + " " + "c");
Console.ReadLine();
 if (Console.ReadLine() == "a")
            {
                Console.WriteLine("comment");
            }
 else if (Console.ReadLine() == "b")
            {
                Console.WriteLine("comment");
            }
 else if (Console.ReadLine() == "c")
            {
                Console.WriteLine("comment");
            }
string str = Console.ReadLine();
switch(str)
{
case "a":
    Console.WriteLine("comment");
    break;
case "b":
    Console.WriteLine("comment");
    break;
case "c":
    Console.WriteLine("comment");
    break;
}

您还可以使用“for”循环显示答案列表:

List<string> answers = new List<string>();

answers.Add("answer 1");
answers.Add("answer 2");
answers.Add("answer 3");

Console.WriteLine("question");

for (int i = 0; i < answers.Count; i++)
    Console.WriteLine((char)('a' + i) + "." + answers[i]);
List answers=新列表();
答案。添加(“答案1”);
答案。添加(“答案2”);
答案。添加(“答案3”);
Console.WriteLine(“问题”);
for(int i=0;i

使用此代码,您将能够清除并填写每个问题的答案列表。

Console.ReadLine()
的结果保存在字符串变量中,并对其进行测试?您需要使用一个变量。将第一个ReadLine的返回值赋给该变量,然后根据常量测试该变量。我是这样做的,不过我可能会切换到switch(呵呵),thanks@alii98当然那会更好;我只是想做最少的改动来让大家了解这个概念。祝你好运谢谢你,祝你度过愉快的一天。为什么有人要用
循环来做这个呢?这甚至不是为这个问题实现
for
循环的好例子。