2012-11-06 4 views
0

콘솔에서 게임을 만들려고하지만 플레이어에게 A 또는 B를 입력하라는 메시지가 표시되면 응용 프로그램이 닫힙니다. 그것은 안된다!사용자 입력 후 응용 프로그램이 닫힙니다.

두 클래스가 있습니다.

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

    } 
} 
} 

사람이 도와주세요 수 :

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입니다 :이 사람은 Window.cs입니까? 사전에 큰 감사드립니다.

답변

4

왜 Environment.Exit (0)을 가지고 있습니까? 거기에? 플레이어 A가 이동하면이 상태의 마지막 코드 줄이됩니다.이 코드는 아무 것도 닫지 않아야합니다.

+0

오 즉. 그러나 플레이어가 옵션 B를 선택하면 게임을 계속 진행해야합니다. 게임은 플레이어가 무엇을 하든지 상관없이 닫힙니다. 플레이어가 A를 선택하더라도 적어도 게임이 종료되기 전에 메시지를 표시해야합니다. 무슨 뜻인지 알 겠어? 편집 : 심지어 Environment.Exit (0) 제거하려고했습니다; 플레이어가 선택하자마자 닫힙니다. – user1779342

+0

물론 닫히고, 실행해야 할 것이 없습니다. 최선의 방법은 다른 대답에 따라 루프에서 사용 입력을 포착하는 것입니다. – Nick

2

현재 입력 문자열을 읽을 때 다음 코드가 없기 때문에 콘솔 창이 닫힙니다. 당신이해야 할 일은 Q를 누를 때까지 입력 키를 읽는 로직 위에 while 루프를 만드는 것입니다. 플레이어가 잘못된 선택하고 그/그녀가 손실 때문에 다음 게임이 종료 옵션 A를 선택하기 때문에

class Program 
{ 
    static void Main(string[] args) 
    { 
     string playerInput = ""; 
     while(playerInput!="q"){ 
      //your code 
      playerInput = Console.ReadLine(); 
     } 
    } 
} 
관련 문제