0

コンソールでゲームを構築しようとしていますが、プレーヤーが A または B のいずれかを入力するよう求められた後、アプリケーションが閉じます。それはいけません!

私は2つのクラスを持っています。これは Window.cs です。

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();

    }
}
}

誰でも助けてもらえますか?よろしくお願いします。

4

2 に答える 2

4

Environment.Exit(0); があるのはなぜですか。そこで?プレイヤー A が自分の動きをするとき、これはその状態のコードの最後の行です...それを閉じる必要があるのは理にかなっていますか?

于 2012-11-06T15:56:34.667 に答える
2

現在、入力文字列が読み取られると、次のコードがないため、コンソール ウィンドウが閉じます。あなたがすべきことは、たとえばQが押されるまで入力キーを読み取るロジックの上にwhileループを作成することです。

class Program
{
    static void Main(string[] args)
    {
        string playerInput = "";
        while(playerInput!="q"){
            //your code
            playerInput = Console.ReadLine();
        }
    }
}
于 2012-11-06T15:59:47.947 に答える