简体   繁体   English

C#循环直到给出有效答案

[英]C# loop until valid answer is given

I'm trying to make a text adventure type game, and I am very new to C#. 我正在尝试制作文字冒险类游戏,而我对C#还是陌生的。 I've searched the web for about half an hour and found nothing that has worked. 我已经在网上搜索了大约半小时,却发现没有任何效果。 I want to make something that will not continue until a valid answer is input 我想做的事情要等到输入有效答案后才能继续

This is an example: 这是一个例子:

var answer3 = Console.ReadLine();

if (answer3.Equals("1"))
{
   Console.WriteLine("ans 1");
}
else if (answer3.Equals("2"))
{
   Console.WriteLine("Ans 2");
}
else
{
   Console.WriteLine("Answer decision 1,2, or 3"); //This is what I need repeated
do{

    var answer3 = Console.ReadLine();


}while(answer3 != "something" || answer3 != "something else");

|| || means 'or' 表示“或”

do- while basically executes something at least once, until some condition is true, the while. do-while基本上至少执行一次操作,直到某些条件成立为止。

do{

 this

}while(this isn't true);

You can use a while loop 您可以使用while循环

string answer = Console.ReadLine();
while(!String.Equals(answer, desiredAnswer)
{
  Console.WriteLine("Answer decision 1,2, or 3");
  answer = Console.ReadLine();  
}

If you have multiple desired answers, you can just OR them in the while condition 如果您有多个所需的答案,则可以while条件while OR

One way to do it that is very C#/.Net is to use Lambda expressions. C#/。Net的一种实现方法是使用Lambda表达式。 You define the list of valid values in an array and it will loop until you get one of these values. 您在数组中定义有效值的列表,该列表将循环播放,直到获得这些值之一为止。

string myString = "";
string[] validValues = new string[] { "1", "2", "3" };
while (!validValues.Any(myString.Equals))
    myString = Console.ReadLine();

It is equivalent to: 它等效于:

string myString = "";
string[] validValues = new string[] { "1", "2", "3" };
while (!validValues.Any(s=>myString.Equals(s))
    myString = Console.ReadLine();

switch (myString)
{
    case "1":
        Console.WriteLine("ans 1");
        break;
    case "2":
        Console.WriteLine("ans 2");
        break;
    case "3":
        Console.WriteLine("ans 3");
        break;
 }

validValues.Any will return true if the condition in brackets pass on any element. 如果括号中的条件传递到任何元素上,则validValues.Any将返回true。 The Lambda expression s=>myString.Equals(s) is almost like a function that will return true when the argument s is equal to myString. Lambda表达式s=>myString.Equals(s)几乎就像一个函数,当参数s等于myString时将返回true。 s will be replaced by each element of the list when using Any . 使用Any时, s将被列表中的每个元素替换。

In this case it is important to notice that => does not mean greater or equal. 在这种情况下,请务必注意=>并不等于或大于等于。 It means that you defined a function/expression that takes s as parameter and returns myString.Equals(s) . 这意味着您定义了一个以s为参数并返回myString.Equals(s)的函数/表达式。 Then you pass it to Any , which will apply it on every element until one returns true . 然后,将其传递给Any ,它将在每个元素上应用它直到一个返回true为止。 If none returned true, Any will return false . 如果没有返回true,则Any将返回false

Lambda expressions are a very neat feature of C#, when you will get used to it, it will allow you to write code which is much simpler than in languages like C++. Lambda表达式是C#的一个非常巧妙的功能,当您习惯了C#时,它将允许您编写比C ++之类的语言简单得多的代码。 Since most of us spend alot of time writing boilerplate code or manipulating data obtained from various sources it can make our life alot easier. 由于我们大多数人都花大量时间编写样板代码或处理从各种来源获得的数据,因此可以使我们的生活更加轻松。

More on lambda expressions 有关Lambda表达式的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM