2011-04-25 21 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 2011-04-25 22:32:40

+3

爲什麼你使用while循環呢?如果用戶回答否,然後將覆蓋設置爲空,爲什麼會拋出異常? – esrange 2011-04-25 22:32:44

+0

@esrange在輸入有效的選項之前,它似乎是指ReadLine(),然後通過將覆蓋設置爲null來從循環中斷開。 @ale break;是擺脫循環的方式。 – adorablepuppy 2011-04-25 22:35:26

回答

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 2011-04-25 22:51:07

+2

此代碼應該在控制檯應用程序中正常工作(儘管ataman,我認爲它是'ToUpper()',而不是'toUpper()')...將其轉換爲大寫處理案例差異;你不需要考慮這種方法的情況。其他可以用於不區分大小寫的字符串比較的方法包括:overwrite.Equals(「Y」,StringComparison.CurrentCultureIgnoreCase),Overwrite.Equals(「Y」,StringComparison.InvariantCultureIgnoreCase)','string.Equals(overwrite, 「Y」,StringComparison.CurrentCultureIgnoreCase);''或簡單地'string.Equals(overwrite,「Y」,true);' – 2011-04-26 19:51:04

+0

是的,ToUpper和Equals都是大寫字母,我總是在java和c#之間跳轉時混合起來。 – atamanroman 2011-04-26 22:20:22

0

嘗試使用break;打出來的循環(也使用的if-else-IF,而不是如果,如果......)

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

雖然 「N」 是無用的,但我希望你以後break;處理你在其他地方拋出的異常。

+2

你真的不需要第三個「if」。 Elses已經照顧它。拋出''''之後,你可能也不需要這兩行中的任何一行。無論如何他們將無法到達。 – Timwi 2011-04-25 22:49:42

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!!"); 
      } 
     } 
相關問題