C# 应用程序在用户输入后关闭
我试图在控制台上构建一个游戏,但当要求玩家输入a或B后,应用程序关闭。不应该 我有两节课。这是Window.cs:C# 应用程序在用户输入后关闭,c#,C#,我试图在控制台上构建一个游戏,但当要求玩家输入a或B后,应用程序关闭。不应该 我有两节课。这是Window.cs: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyAdventure { public class Window { public static void theWindow() { string pla
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyAdventure
{
public class Window
{
public static void theWindow()
{
string playerInput = Console.ReadLine();
Console.Clear();
Console.WriteLine("You approach the window, and you look outside.");
Console.WriteLine("You see lots of trees, but you can't put a name on the location.");
Console.WriteLine("You pull the handle on the window in an attempt to open it.");
Console.WriteLine("You succeed and escape the room through the window.");
Console.WriteLine("\nAs you look around, you still don't know where you are.");
Console.WriteLine("You suddenly hear someone approaching from behind.");
Console.WriteLine("You turn around and see a man drenched in blood.\nHe's pale, and he looks sick. He moans at you.");
Console.WriteLine("\n\nWhat do you do?\nA. Ask if he's alright and who he is\nB. Try to find something tod defend yourself with, just in case...");
if (playerInput.StartsWith("A", StringComparison.CurrentCultureIgnoreCase))
{
Console.WriteLine("You ask if he's alright, but he doesn't respond.\nYou ask then who he is, but he still doesn't respond.");
Console.WriteLine("Before you even get to think, he lunges towards you in a surprise!");
Console.WriteLine("He holds on to you and tries to bite you.\nUnfortunately, he manages to do so and takes a chunk from your neck.");
Console.WriteLine("As he's consuming you, you scream aloud in pain as you slowly fade away...");
Console.WriteLine("\n\nGAME OVER!\nUnfortunately, your choices got you killed.\nPlease, restart the game and try again.");
Console.WriteLine("\n\n(Press any key to exit");
Console.ReadKey();
Environment.Exit(0);
}
else if (playerInput.StartsWith("B", StringComparison.CurrentCultureIgnoreCase))
{
//Insert next class and method here...
}
}
}
}
我的另一门课是Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyAdventure
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("You cannot feel your body. You don't know where you are.\nThe room you're in is dark. You cannot see much.");
Console.WriteLine("You decide to stand up, and take a look around. You see a door and a window.\nWhich do you check out first?");
Console.WriteLine("A. Window\nB. Door");
string playerInput = Console.ReadLine();
if (playerInput.StartsWith("A", StringComparison.CurrentCultureIgnoreCase))
{
Window.theWindow();
}
else if (playerInput.StartsWith("B", StringComparison.CurrentCultureIgnoreCase))
{
Console.Clear();
Console.WriteLine("You chose the door.");
Console.WriteLine("\nUnfortunately, the door is locked.\nYou cannot leave through the door.");
Console.WriteLine("You turn to the window.");
Console.WriteLine("\n(Press Enter to continue...)");
Console.ReadKey();
Window.theWindow();
}
Console.ReadKey();
}
}
}
有人能帮忙吗?提前向您致谢。您为什么有环境。退出(0);在那里?当玩家A移动时,这是该情况下的最后一行代码。。。它应该关闭是有意义的否?为什么有环境。退出(0);在那里?当玩家A移动时,这是该情况下的最后一行代码。。。它应该关闭no?当当前读取输入字符串时,控制台窗口关闭,因为没有下一个代码。您应该做的是在逻辑上方创建一个while循环,该循环将读取输入键,直到按下Q为止
class Program
{
static void Main(string[] args)
{
string playerInput = "";
while(playerInput!="q"){
//your code
playerInput = Console.ReadLine();
}
}
}
当前,当读取输入字符串时,控制台窗口关闭,因为没有下一个代码。您应该做的是在逻辑上方创建一个while循环,该循环将读取输入键,直到按下Q为止
class Program
{
static void Main(string[] args)
{
string playerInput = "";
while(playerInput!="q"){
//your code
playerInput = Console.ReadLine();
}
}
}
哦,那是因为玩家选择了选项A,这是错误的选择,然后游戏结束,因为他/她输了。但是当玩家选择B选项时,游戏应该继续。无论玩家做什么,游戏都会结束。即使玩家选择了A,它至少应该在游戏结束前显示消息。你明白我的意思吗?编辑:我甚至尝试删除环境。退出(0);当玩家做出选择时,它仍然关闭。当然,它关闭了,没有什么可以执行的了。你最好的选择是按照另一个答案在循环中捕获使用输入。哦,那是因为玩家选择了选项a,这是错误的选择,然后游戏因为他/她输了而结束。但是当玩家选择B选项时,游戏应该继续。无论玩家做什么,游戏都会结束。即使玩家选择了A,它至少应该在游戏结束前显示消息。你明白我的意思吗?编辑:我甚至尝试删除环境。退出(0);当玩家做出选择时,它仍然关闭。当然,它关闭了,没有什么可以执行的了。您的最佳选择是根据另一个答案在循环中捕获并使用输入。