2017-01-26 4 views
3

C#をもっと学ぶためにコンソールベースのテキストゲームを作っていますが、今はこだわっていてSOに頼っています。コンソールキー操作が機能していませんか?

これは明らかな答えかもしれませんが、私は私を助けるためにもう一組の目が必要です。私はここで他の質問をチェックして、彼らは私を助けるように見えません。私Main()

、私は次のようにあります

 int age; 

     Console.Write("Before entering the unknown, could you please confirm your age? "); 
     age = Convert.ToInt32(Console.ReadLine()); 
     Console.WriteLine(""); 

     if (age < 18) 
     { 
      Console.WriteLine("I'm sorry, you'll now be transferred to a non-explicit version."); 
      Console.WriteLine("\n<Press any key to continue>"); 
      Console.ReadKey(); 
      Console.Write("\nPlease wait..."); 
      for (int t = 5; t >= 0; t--) 
      { 
       Console.CursorLeft = 22; 
       Console.Write("{0:00}" + " seconds remaining", t); 
       Thread.Sleep(1000); 
      } 
      Console.WriteLine(""); 
      Console.WriteLine("\nTo proceed, please press any key..."); 
      Console.ReadLine(); 

      NonExplicit(); 
     } 

自明。それはNonExplicit()方法を打つたら、次が呼び出されます。

1)それがヒットするように入力が必要です。

 char yes = Console.ReadKey().KeyChar; 
     string name; 

     Console.WriteLine("\nYou're awake? It's about time. Not old enough, eh? That sucks. \n\nListen, I really need your help. Would you be interested? Y/N"); 
     Console.ReadKey(); 

     if (yes == 'y' || yes == 'Y') 
     { 
      Console.Write("\nYou will?! Wow, That's fantastic. Right then, where shall I start? \nI know! How about telling me your name? "); 
      name = Console.ReadKey().ToString(); 
      Console.WriteLine("\nAhh yes, now you mention it, I certainly remember you, {0}!", name); 
     } 
     else 
     { 
      Console.WriteLine("\nYou don't want to help me? Oh, that's unfortunate... "); 
     } 

起こるようです何「Y」または「Y」が押されたときに、ということです上記を登録してください。

2)if & elseWriteLinesが実行されます。これは1)と組み合わせて使用​​すると仮定していますか?

「y/Y」を押すと、正しい基準が読み取られるように、コードをどのように修正できますか?またはEnterを押して続行する必要性を取り除くにはどうすればよいですか?

+2

'char yes = Console.ReadKey()。KeyChar;'次に 'WriteLine'の後に別の' Console.ReadKey(); '? 2番目の 'ReadKey()'を置き換えるには 'char yes = Console.ReadKey()。KeyChar;'を動かしてみてください。 –

答えて

1

は、私はそれをテストし、それが動作する、以下のコードを使用します。

私はchar yes = Console.ReadKey()Console.WriteLine()の下に移動しました。
name = Console.ReadKey()Console.ReadLine()に変更しました。名前が1より大きいためです。

 string name; 
     Console.WriteLine("\nYou're awake? It's about time. Not old enough, eh? That sucks. \n\nListen, I really need your help. Would you be interested? Y/N"); 
     char yes = Console.ReadKey(); 

     if (yes == 'y' || yes == 'Y') 
     { 
      Console.Write("\nYou will?! Wow, That's fantastic. Right then, where shall I start? \nI know! How about telling me your name? "); 
      name = Console.ReadLine(); 
      Console.WriteLine("\nAhh yes, now you mention it, I certainly remember you, {0}!", name); 
      Console.ReadKey(); // I put this here so the program wouldn't exit 
     } 
     else 
     { 
      Console.WriteLine("\nYou don't want to help me? Oh, that's unfortunate... "); 
     } 
+0

これは完全に機能しました。 ありがとうございました。 – Sean

+1

問題なく、動作するコードスニペットを提供してくれてありがとうございます:)それが私たちがすべてあなたを助けることができる理由です。 – EpicKip

0
char yes = Console.ReadKey(); // No need for .KeyChar 

int型の年齢;

Console.Write( "未知数を入力する前に、あなたの年齢を確認できますか?"); 年齢= Convert.ToInt32(Console.ReadLine()); Console.WriteLine( ""); 。

int age = -1 
Console.WriteLine("Before entering the unknown, could you please confirm your age? "); 
while (age == -1) 
{ 
    if !(int.TryParse(Console.ReadLine(), out age)) 
    { 
     Console.WriteLine("Please enter a valid age"); 
    } 
} 

名= Console.ReadKey()のToString()とすることができます。 (はい== 'Y' ||はい== 'Y')

がIn

if (yes.ToUpper() == "Y") 
1

することができれば

name = Console.ReadLine(); 

なければなりませんあなたのコードはConsole.WriteLine("\nTo proceed, please press any key...");の後にConsole.ReadLine(); eコンソールに行を入力することができます(Enterを押すまでは読んではいけません)。そして、2番目の方法では、もう一度コンソールを読み込もうとしています。

最初にConsole.ReadLine()を削除してもよろしいですか?

そして、あなたは(あなたの質問をする前に、別のReadKeyを行う前に、イエス/ノーの答えを得るしないようにしようとしている。

1
string result = Console.ReadLine(); 
if (result.ToUpper().Equals("Y")) 
    { 
    //any thing 
    } 
関連する問題