如何让控制台读取所有条件
How to get a console to read through all conditions
我有一个控制台应用程序,要求用户从三个选项中选择一个,并在需要时能够打开库存。但是,控制台不会检查其他条件是否为真或假,而是线性读取条件并等待第一个条件满足。因此,例如,在下面的代码中,它 运行 是文本的第一位,显示选项,然后等待用户输入 "inventory" 而不考虑其他选项。这是怎么回事?为什么会这样?以及如何让控制台 运行 通过并检查是否满足所有条件?
这是代码
using System;
using System.IO;
namespace Feed_de_monky
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
string one = "";
string two = "";
string inventory = "inventory";
int storyint = 0;
TextReader input = Console.In;
TextWriter output = Console.Out;
int options = 0;
if (storyint == 0)
{
Console.WriteLine("You are in a dark Jungle. You look into the darkness of the trees and see the silhouette of a tiger standing in front of you down the way.");
Console.WriteLine("");
Console.WriteLine("turn and run");
Console.WriteLine("pounce on tiger");
Console.WriteLine("climb a tree.");
options++;
if (input.ReadLine() == inventory)
{
output.WriteLine(one);
output.WriteLine(two);
return;
}
else if(input.ReadLine() == "turn and run" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger chases you through the darkness. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
else if(input.ReadLine() == "pounce on tiger" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger is caught by surprise. You overwhelm the beast and he dies of shock and surprise on the spot");
one = "tiger skin";
output.WriteLine("TIGER SKIN ADDED TO YOUR INVENTORY");
storyint++;
options++;
}
else if(input.ReadLine() == "climb a tree" && options == 1)
{
output.WriteLine("");
output.WriteLine("You climb the tree. But while you sit on the branches believing yourself to be safe, the tiger jumps through the air and bites your head clean off. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
}
}
}
我想你可能需要设置
var inputLine = input.ReadLine();
然后在变量 inputLine 上执行您的逻辑。
正如您现在拥有的那样,我相信它会比您预期的调用 ReadLine 的次数更多。但是,如果您只调用 .ReadLine() 一次并将其分配给一个变量,这应该比重复调用它更好。
我有一个控制台应用程序,要求用户从三个选项中选择一个,并在需要时能够打开库存。但是,控制台不会检查其他条件是否为真或假,而是线性读取条件并等待第一个条件满足。因此,例如,在下面的代码中,它 运行 是文本的第一位,显示选项,然后等待用户输入 "inventory" 而不考虑其他选项。这是怎么回事?为什么会这样?以及如何让控制台 运行 通过并检查是否满足所有条件?
这是代码
using System;
using System.IO;
namespace Feed_de_monky
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
string one = "";
string two = "";
string inventory = "inventory";
int storyint = 0;
TextReader input = Console.In;
TextWriter output = Console.Out;
int options = 0;
if (storyint == 0)
{
Console.WriteLine("You are in a dark Jungle. You look into the darkness of the trees and see the silhouette of a tiger standing in front of you down the way.");
Console.WriteLine("");
Console.WriteLine("turn and run");
Console.WriteLine("pounce on tiger");
Console.WriteLine("climb a tree.");
options++;
if (input.ReadLine() == inventory)
{
output.WriteLine(one);
output.WriteLine(two);
return;
}
else if(input.ReadLine() == "turn and run" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger chases you through the darkness. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
else if(input.ReadLine() == "pounce on tiger" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger is caught by surprise. You overwhelm the beast and he dies of shock and surprise on the spot");
one = "tiger skin";
output.WriteLine("TIGER SKIN ADDED TO YOUR INVENTORY");
storyint++;
options++;
}
else if(input.ReadLine() == "climb a tree" && options == 1)
{
output.WriteLine("");
output.WriteLine("You climb the tree. But while you sit on the branches believing yourself to be safe, the tiger jumps through the air and bites your head clean off. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
}
}
}
我想你可能需要设置
var inputLine = input.ReadLine();
然后在变量 inputLine 上执行您的逻辑。
正如您现在拥有的那样,我相信它会比您预期的调用 ReadLine 的次数更多。但是,如果您只调用 .ReadLine() 一次并将其分配给一个变量,这应该比重复调用它更好。