2011-04-25 1 views
2

파일이 존재할 때 "파일이 존재합니다 ... 덮어 쓰시겠습니까?"라는 메시지를 표시해야합니다.사용자 옵션 예/아니요

if (File.Exists(binaryFilePath)) 
{ 
    Program.DisplayMessage("The file: " + binaryFileName + " exist. You want to overwrite it? Y/N"); 
    string overwrite = Console.ReadLine(); 
    while (overwrite != null) 
    { 
     if (overwrite.ToUpper() == "Y") 
     { 
     WriteBinaryFile(frameCodes, binaryFilePath); 

     } if (overwrite.ToUpper() == "N") 
     { 
     throw new IOException(); 
     overwrite = null; 
     } if (overwrite.ToUpper() != "Y" && overwrite.ToUpper() != "N") 
     { 
     Program.DisplayMessage("!!Please Select a Valid Option!!"); 
     overwrite = Console.ReadLine(); 
     } 
    } 
} 

사용자가 "Y"로 프로세스를 시작하고 정상적으로 처리하면 ... 문제는 어떻게 멈출 수 있습니까 ?? 나는이 동안 노력하지만 작동하지 않습니다 ...

어떻게해야합니까?

+1

사람들의 편집을 취소하지 마십시오. 그들은 질문을보다 읽기 쉽고 답할 수 있도록 코드의 형식을 개선하려고 노력하고 있습니다. – ChrisF

+3

왜 while 루프를 사용합니까? 그리고 사용자가 no로 응답 한 다음 null로 덮어 쓰기를 설정하면 예외가 발생하는 이유는 무엇입니까? – esrange

+0

@esrange 유효한 옵션이 입력 될 때까지 ReadLine()을 의미하고, 겹쳐 쓰기를 null로 설정하여 루프에서 벗어나는 것을 의미합니다. @ale break; 루프에서 벗어나는 방법입니다. – adorablepuppy

답변

3
if (File.Exists(binaryFilePath)) 
{ 
    while (true) 
    { 
    Program.DisplayMessage("The file: " + binaryFileName + " already exist. Do you want to overwrite it? Y/N"); 
    string overwrite = Console.ReadLine(); 
    if (overwrite.ToUpper().Equals("Y")) 
    { 
     WriteBinaryFile(frameCodes, binaryFilePath); 
     break; 
    } 
    else if (overwrite.ToUpper().Equals("N")) 
    { 
     Console.WriteLine("Aborted by user."); 
     break; 
    } 
    else 
    { 
     Program.DisplayMessage("!!Please Select a Valid Option!!"); 
     overwrite = Console.ReadLine(); 
     continue; // not needed - for educational use only ;) 
    } 
    } 
} 

기본 사항 (조건, 루프, 영어 등)을 배우십시오. 그런 다음 다시 돌아와 왜 예외 (특히 그 중 하나)를 던지는 것이 귀하의 경우에 잘못된 것인지 물어보십시오.

+0

이 응용 프로그램은 콘솔에 있으며, EqualsIgnoreCase에 대한 사용이 필요합니까 ?? 식별하지 않습니다 ... – ale

+2

이 코드는 콘솔 응용 프로그램에서 제대로 작동합니다. (비록 ataman, 나는 그것이'toUpper()'가 아니라'toUpper()')라고 생각합니다 ... 대문자로 변환하면 대소 문자를 처리합니다. 이 방법으로 사례를 고려할 필요가 없습니다. 대소 문자를 구별하지 않는 문자열 비교를 위해 사용할 수있는 다른 방법은'overwrite.Equals ("Y", StringComparison.CurrentCultureIgnoreCase)','overwrite.Equals ("Y", StringComparison.InvariantCultureIgnoreCase)','string.Equals –

+0

예, ToUpper와 Equals는 모두 대문자이고, 자바와 C# 사이를 돌면서 항상 섞어 버립니다. – atamanroman

0

루프의 탈옥 break;를 사용해보십시오 (또한 사용하는 경우 - 다른-경우가 아닌 경우-경우 ..) "N"은 쓸모하지만 난 당신이 희망 후 break; 비록

if (File.Exists(binaryFilePath)) 
{ 


    while (true) 
    { 
     Program.DisplayMessage("The file: " + binaryFileName + " exist. You want to overwrite it? Y/N"); 
     string overwrite = Console.ReadLine(); 
     if (overwrite.ToUpper() == "Y") 
     { 
      WriteBinaryFile(frameCodes, binaryFilePath); 
      break; 

     } 
     else if (overwrite.ToUpper() == "N") 
     { 
      throw new IOException(); 
      overwrite = null; 
      break; 
     } 
     else if (overwrite.ToUpper() != "Y" && overwrite.ToUpper() != "N") 
     { 
      Program.DisplayMessage("!!Please Select a Valid Option!!"); 
      overwrite = Console.ReadLine(); 
     } 
    } 
} 

다른 곳에서 던지고있는 예외를 처리합니다.

+2

정말 세 번째'if'가 필요하지 않습니다. elses는 이미 그것을 돌본다. 'throw' 이후에 두 줄을 필요로하지 않을 수도 있습니다; 그들은 어쨌든 도달 할 수 없을 것입니다. – Timwi

0

나는 다른 방법으로 사용자 선택 읽기가 위임되어야한다고 생각합니다. 이와 같이 :

static void Main(string[] args) 
     { 
      //... 

      if (File.Exists(binaryFilePath)) 
      { 
       if(ReadBool("The file: " + binaryFileName + " exist. You want to overwrite it? Y/N")) 
        WriteBinaryFile(frameCodes, binaryFilePath); 
       else 
        throw new IOException(); 
      } 
     } 

static bool ReadBool(String question) 
     { 
      while (true) 
      { 
       Console.WriteLine(question); 
       String r = (Console.ReadLine() ?? "").ToLower(); 
       if (r == "y") 
        return true; 
       if (r == "n") 
        return false; 
       Console.WriteLine("!!Please Select a Valid Option!!"); 
      } 
     }