0

MS Visual Studio 2010을 사용하고 프로그램은 C# 콘솔 응용 프로그램을 기반으로합니다.C#이 명령문을 건너 뛰는 이유는 무엇입니까?

나는 sInput = Console.ReadLine(); 및 그 다음에 switch이있는 while 루프를 가지고 있습니다.

내 코드를 실행하면 sInput가 (자동으로 switchdefault 값을 제공)을 while 루프의 첫 번째 항목으로 건너 뜁니다.

그러나 두 번째로 나타나는 While 루프에서 제대로 작동합니다.

왜 처음으로 sInput을 건너 뜁니 까?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Runtime.InteropServices; 
using System.Data; 
using System.Data.OleDb; 
using System.Drawing; 
using System.Drawing.Imaging; 

namespace game_Console 
{ 
    class Program 
    { 
     [DllImport("kernel32.dll", ExactSpelling = true)] 

    private static extern IntPtr GetConsoleWindow(); 
    private static IntPtr ThisConsole = GetConsoleWindow(); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 

    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 
    private const int HIDE = 0; 
    private const int MAXIMIZE = 3; 
    private const int MINIMIZE = 6; 
    private const int RESTORE = 9; 

    static void Main(string[] args) 
    { 
     Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight); 
     ShowWindow(ThisConsole, MAXIMIZE); 

     string sMenu, sIntro, sInput; 
     bool bExit, bCredits; 

     sMenu = "********************\n Pokemon Console\n********************\n\n Type in the desired number(1-5) and press Enter.\n--------------------\n 1. New Game\n 2. How To Play\n 3. High Scores\n 4. Credits\n 5. Exit\n____________________\n"; 
     sIntro = "\n  ######## ######## ## ## ######## ## ## ######## ## ##  ######## ######## ## ## ######## ######## ##  ########\n  ######## ######## ## ## ######## ### ### ######## ### ##  ######## ######## ### ## ######## ######## ##  ########\n  ## ## ## ## ## ## ##  ## ## ## ## ## ### ##  ## ## ## ## ### ## ##  ## ## ##  ##\n  ## ## ## ## ## ##  ##  ## ## ## ## ## ## # ##  ##  ## ## ## # ## ##  ## ## ##  ##\n  ######## ## ## ###  ####  ## ## ## ## ## # ##  ##  ## ## ## # ## ######## ## ## ##  ####\n  ######## ## ## ###  ####  ## ## ## ## ## # ##  ##  ## ## ## # ## ######## ## ## ##  ####\n  ##  ## ## ## ##  ##  ## ## ## ## ## # ##  ##  ## ## ## # ##  ## ## ## ##  ##\n  ##  ## ## ## ## ##  ## ## ## ## ## ###  ## ## ## ## ## ###  ## ## ## ##  ##\n  ##  ######## ## ## ######## ## ## ######## ## ###  ######## ######## ## ### ######## ######## ######## ########\n  ##  ######## ## ## ######## ## ## ######## ## ##  ######## ######## ## ## ######## ######## ######## ########\n ______________________________________________________________________________________________________________________________________________________________\n                  Press Enter to continue"; 
     bExit = false; 
     bCredits = false; 

     Image Picture = Image.FromFile(@"C:\Users\Home\Desktop\pokemon console\Pokemon Console\Pokemon Console\Data\pokemon\pics\00.png"); 
     Console.SetBufferSize((Picture.Width * 0x2), (Picture.Height * 0x2)); 
     FrameDimension Dimension = new FrameDimension(Picture.FrameDimensionsList[0x0]); 
     int FrameCount = Picture.GetFrameCount(Dimension); 
     int Left = Console.WindowLeft, Top = Console.WindowTop; 
     char[] Chars = { '#', '#', '@', '%', '=', '+', '*', ':', '-', '.', ' ' }; 
     Picture.SelectActiveFrame(Dimension, 0x0); 
     for (int i = 0x0; i < Picture.Height; i++) 
     { 
      for (int x = 0x0; x < Picture.Width; x++) 
      { 
       Color Color = ((Bitmap)Picture).GetPixel(x, i); 
       int Gray = (Color.R + Color.G + Color.B)/0x3; 
       int Index = (Gray * (Chars.Length - 0x1))/0xFF; 
       Console.Write(Chars[Index]); 
      } 
      Console.Write('\n'); 
     } 


     Console.WriteLine(sIntro); 
     Console.Read(); 
     Console.Clear(); 

     while (bExit == false) 
     { 
      Console.Clear(); 
      Console.WriteLine(sMenu); 
      sInput = Console.ReadLine();//Skips this line in the first occurrence 

      switch (sInput) 
      { 
       case "1": 
       { 
        Console.WriteLine("one"); 
        Console.ReadLine(); 
        break; 
       } 
       case "2": 
       { 
        Console.WriteLine("two"); 
        Console.ReadLine(); 
        break; 
       } 
       case "3": 
       { 
        Console.WriteLine("three"); 
        Console.ReadLine(); 
        break; 
       } 
       case "4": 
       { 
        Console.WriteLine("four"); 
        Console.ReadLine(); 
        break; 
       } 
       case "5": 
       { 
        bExit = true; 
        break; 
       } 
       default: 
       { 
        Console.WriteLine("error"); 
        Console.ReadLine(); 
        break; 
       }      
      } 

     } 

     Console.Clear(); 
     if (bCredits == false) 
     {     
      Console.WriteLine(" Take a look at our Credits before you leave"); 
      TextReader tr = new StreamReader("Credits.txt"); 
      Console.WriteLine(tr.ReadToEnd()); 
      tr.Close(); 
     } 
     Console.WriteLine("\n +++++++++++++++++++++++++\n ++Thank you for playing++\n +++++++++++++++++++++++++\n\n Press Enter to Exit"); 
     Console.Read(); 
     Console.Read(); 
    } 
} 
} 

편집는 : 문제는 while 루프 외부 이전 코드에서 유래 경우 설정 내 모든 코드를 추가했습니다.

+1

이 코드를 테스트 나를 위해 작동합니다

전에 라인을 건너 뛴 다음 코드를 추가하십시오. 어쩌면 코드가 실제로 변경 사항을 컴파일하지 않고 있을까요? – Cyral

+0

'switch (sInput)'에 중단 점을 추가하고'sInput'의 값을 확인 하시겠습니까? –

+0

중단 점 뒤에'sInput'의 값은'' "입니다. – sgtBlueBird

답변

1

내 생각 엔 입력 버퍼가 비어 있지 않고 개행을 포함하고 있습니다. 제공되는 코드가 다른 데이터 입력이 발생하는 상황에서 벗어 났을 수 있습니까?

while (Console.KeyAvailable) 
    Console.ReadKey(true); 
+0

줄은 계속 건너 뜁니다. – sgtBlueBird

+1

''while' 루프 이전에 첫 번째'Console.Read();를 전달하기 위해 ENTER 키를 사용하지 않습니까? 그렇다면 다른 문자 (예 : 'a')를 치고 작동하는지 확인하십시오. –

+0

언급했듯이'Console.Read()'를'Console.ReadLine()'으로 변경했습니다. – sgtBlueBird

관련 문제