2009-06-14 222 views
1

嗨,我正在練習程序中的「開關循環」。我正在創建一個代碼,用戶可以輸入整數,在用戶輸入整數後,它也會顯示用戶輸入的內容。現在我試圖在程序要求用戶輸入通過選擇Y/N再次輸入號碼。我已經將它包含在我的代碼中,但如果我在程序第一次輸入字符時鍵入一個整數,程序將執行catch部分。我怎樣才能讓它,如果用戶將鍵入一個字符,它也將顯示消息,「請輸入整數:」switch語句

感謝您的幫助我很迷茫在這裏的某個地方。

  int enterYourNumber; 
      char shortLetter; 

      try 
      { 
       Console.WriteLine("Please enter the integer: "); 
       enterYourNumber = Convert.ToInt32(Console.ReadLine()); 
       WriteNumber(enterYourNumber); 

       Console.WriteLine("Do you still want to enter a number? Y/N"); 
       shortLetter = Convert.ToChar(Console.ReadLine()); 

       while (shortLetter == 'y' || shortLetter == 'Y') 
       { 
        Console.WriteLine("Please enter the integer: "); 
        enterYourNumber = Convert.ToInt32(Console.ReadLine()); 
        WriteNumber(enterYourNumber); 

        Console.WriteLine("Do you still want to enter a number? Y/N"); 
        shortLetter = Convert.ToChar(Console.ReadLine()); 
       } 

      } 
      catch 
      {           
       Console.WriteLine("Please enter an integer not a character"); 
      } 
     } 

      public static void WriteNumber(int wordValue) 
      { 

      switch (wordValue) 
      { 
       case 1: 
        Console.WriteLine("You have entered number one"); 
        break; 
       case 2: 
        Console.WriteLine("You have entered number two"); 
        break; 
       case 3: 
        Console.WriteLine("You have entered number three"); 
        break; 
       default: 
        Console.WriteLine("You have exceeded the range of number 1-3 "); 
        break; 
      } 

======================

這是我做的,我不知道爲什麼我得到一個錯誤。新的方法似乎不起作用:

 int enterYourNumber; 
     char shortLetter; 


      do 
     { 
      enterYourNumber = GetNumber(); 
      WriteNumber(enterYourNumber);     
      Console.WriteLine("Do you still want to enter a number? Y/N"); 
      shortLetter = Convert.ToChar(Console.ReadLine()); 
     } 
     while (shortLetter == 'y' || shortLetter == 'Y') 
     { 
      Console.WriteLine("Please enter the integer: "); 
      enterYourNumber = Convert.ToInt32(Console.ReadLine()); 
      WriteNumber(enterYourNumber); 

      Console.WriteLine("Do you still want to enter a number? Y/N"); 
      shortLetter = Convert.ToChar(Console.ReadLine()); 
     } 
    } 



     public static int GetNumber() 
     { 
     bool done = false; 
     int value; 
     while (!done) 
     { 
      Console.WriteLine("Please enter the integer: "); 
     try 
     { 
      value = Convert.ToInt32(Console.ReadLine()); 
      done = true; 
     } 
     catch 
     { 
      Console.WriteLine("Please enter an integer not a character"); 
     } 
     } 
     } 

=============

你好比爾,這是你給的例子,似乎我仍然得到一個錯誤:你可以請代碼。再次

public static void Main(string[] args) 
    { 
     int enterYourNumber; 
     char shortLetter; 



     do 
     { 
      enteryourNumber = GetNumber(); 
      WriteNumber(enteryourNumber); 
      Console.WriteLine("Do you still want to enter a number? Y/N"); 
      shortLetter = Convert.ToChar(Console.ReadLine()); 
     } 
     while (shortLetter == 'y' || shortLetter == 'Y'); 
    } 


    public static int GetNumber() 
    { 
     bool done = false; 
     int value; 
     while (!done) 
     { 
      Console.WriteLine("Please enter the integer: "); 
      try 
      { 
       value = Convert.ToInt32(Console.ReadLine()); 
       done = true; 
      } 
      catch 
      { 
       Console.WriteLine("Please enter an integer not a character"); 
      } 

      Console.WriteLine("Please enter the integer: "); 
      enterYourNumber = Convert.ToInt32(Console.ReadLine()); 
      WriteNumber(enterYourNumber); 
      Console.WriteLine("Do you still want to enter a number? Y/N"); 
      shortLetter = Convert.ToChar(Console.ReadLine()); 
     } 
    } 

    public static void WriteNumber(int wordValue) 
    { 

     switch (wordValue) 
     { 
      case 1: 
       Console.WriteLine("You have entered number one"); 
       break; 

      case 2: 
       Console.WriteLine("You have entered number two"); 
       break; 

      case 3: 
       Console.WriteLine("You have entered number three"); 
       break; 

      default: 
       Console.WriteLine("You have exceeded the range of number 1-3 "); 
       break; 
     } 
    } 
} 

}

=================

你好這個問題是羅伯特:非常感謝你。這就是我現在所做的,但是如果我輸入「N」,它不會退出程序。它仍然問了同樣的問題。任何想法:感謝:

public static void Main(string[] args) 
    { 
     int enterYourNumber; 
     char shortLetter; 
     bool validEntry; 



     while (true) 
     { 
      do 
      { 
       Console.WriteLine("Please enter an integer: "); 
       string numberString = Console.ReadLine(); 
       validEntry = int.TryParse(numberString, out enterYourNumber); 
       WriteNumber(enterYourNumber); 
       if (!validEntry) 
       { 
        Console.WriteLine("Entry must be an integer"); 
       } 
      } while (!validEntry); 

      Console.WriteLine("Do you still want to enter a number? Y/N"); 
      shortLetter = Convert.ToChar(Console.ReadLine()); 


     } 
    } 




    public static void WriteNumber(int wordValue) 
    { 
     switch (wordValue) 
     { 
      case 1: 
       Console.WriteLine("You have entered number one"); 
       break; 
      case 2: 
       Console.WriteLine("You have entered numbered two"); 
       break; 
      case 3: 
       Console.WriteLine("You have entered numbered three"); 
       break; 
      default: 
       Console.WriteLine("You have exceeded the range of number 1-3"); 
       break; 
     } 
    } 
} 

}

================================= ===

嗨羅伯特和比爾這裏是我現在得到的。希望你能提出一些關於如何改進編碼的想法。謝謝你的幫助。

 public static void Main(string[] args) 
    { 
     int intEnterYourNumber; 
     char charShortLetter; 
     string strUserInput; 

     do 
     { 
      do 
      { 
       Console.WriteLine("Please enter the integer: "); 
       strUserInput = Console.ReadLine(); 
      } while (!int.TryParse(strUserInput, out intEnterYourNumber)); 
      WriteNumber(intEnterYourNumber); 
      Console.WriteLine("Do you still want to enter a number? Y/N"); 
      charShortLetter = Convert.ToChar(Console.ReadLine().ToUpper()); 
     } while (charShortLetter == 'Y'); 
    } 



    public static void WriteNumber(int wordValue) 
    { 
     switch (wordValue) 
     { 
      case 1: 
       Console.WriteLine("You have entered number one"); 
       break; 
      case 2: 
       Console.WriteLine("You have entered numbered two"); 
       break; 
      case 3: 
       Console.WriteLine("You have entered numbered three"); 
       break; 
      default: 
       Console.WriteLine("You have exceeded the range of number 1-3"); 
       break; 
     } 
    } 

回答

0

移動循環內的try/catch塊。 Robert Harvey對do/while循環的建議也很好。

int enterYourNumber; 
char shortLetter; 

do 
{ 
     try 
     { 
      Console.WriteLine("Please enter the integer: ");     
      enterYourNumber = Convert.ToInt32(Console.ReadLine());     
      WriteNumber(enterYourNumber); 
     } 
     catch(FormatException) 
     {           
      Console.WriteLine("Please enter an integer not a character"); 
     } 

     shortLetter = '\0'; 
     do 
     { 
      try 
      { 
       Console.WriteLine("Do you still want to enter a number? Y/N");      
       shortLetter = Convert.ToChar(Console.ReadLine()); 
      } 
      catch(FormatException) 
     { 
       Console.WriteLine("Please enter a single character"); 
     } 
     }  
     while (shortLetter == '\0')  
}     
while (shortLetter == 'y' || shortLetter == 'Y') 

} 

     public static void WriteNumber(int wordValue) 
     { 

     switch (wordValue) 
     { 
      case 1: 
       Console.WriteLine("You have entered number one"); 
       break; 
      case 2: 
       Console.WriteLine("You have entered number two"); 
       break; 
      case 3: 
       Console.WriteLine("You have entered number three"); 
       break; 
      default: 
       Console.WriteLine("You have exceeded the range of number 1-3 "); 
       break; 
     } 
2
do 
{ 
    Console.WriteLine("Please enter the integer: ");     
    enterYourNumber = Convert.ToInt32(Console.ReadLine());     
    WriteNumber(enterYourNumber);     
    Console.WriteLine("Do you still want to enter a number? Y/N");      
    shortLetter = Convert.ToChar(Console.ReadLine()); 
}     
while (shortLetter == 'y' || shortLetter == 'Y') 
+0

感謝羅伯特怎麼樣,如果用戶將輸入的第一次一個角色? – tintincutes 2009-06-14 01:16:14

+0

把這個做/做在你的try/catch中,就像在原始代碼中一樣。 – 2009-06-14 01:24:15

+0

不幸的是,它並沒有讓用戶在發生異常後回到循環中,這是一個問題。 – billjamesdev 2009-06-14 02:16:00

1

羅伯特是一個開始,但也許我們還是想嘗試捕捉...

替換爲您的主要功能:

do 
{ 
    enterYourNumber = GetNumber(); 
    WriteNumber(enterYourNumber);     
    Console.WriteLine("Do you still want to enter a number? Y/N"); 
    shortLetter = Convert.ToChar(Console.ReadLine()); 
} 
while (shortLetter == 'y' || shortLetter == 'Y') 

,並添加此功能:

public static int GetNumber() { 
    boolean done = false; 
    int value; 
    while (!done) { 
     Console.WriteLine("Please enter the integer: "); 
     try { 
      value = Convert.ToInt32(Console.ReadLine()); 
      done = true; 
     } 
     catch { 
      Console.WriteLine("Please enter an integer not a character"); 
     } 
    } 
} 

然後刪除您的try catch主要功能。

+0

嗨比爾請看我編輯的答案,看來新方法在這裏有問題 – tintincutes 2009-06-14 01:33:36

8

我覺得你的代碼會更容易學習和理解,如果你沒有使用異常來測試你的號碼。嘗試使用int.TryParse()代替(MSDN上的TryParse())。的TryParse()取決於輸入的號碼是否有效返回(或):

int number; 
bool validEntry = int.TryParse(enterYourNumber, out number); 
if (!validEntry) 
{ 
    Console.WriteLine("Entry must be an integer."); 
} 

此外,瞭解do ... while循環link)。 do..while循環類似於常規的while循環,除了在計算條件表達式之前執行do-while循環一次。

bool validEntry; 
int enteredNumber; 
do 
{ 
    Console.Write("Please enter the integer: "); 
    string numberString = Console.ReadLine(); 
    validEntry = int.TryParse(numberString, out enteredNumber); 
    if (!validEntry) 
    { 
     Console.WriteLine("Entry must be an integer."); 
    } 
} while (!validEntry); 

裹在另一個while循環,整個事情(「你還是要輸入數字(Y/N)?」),你就大功告成了。

享受,

羅伯特C. Cartaino