2015-09-20 92 views
2

我想轉換我的整數,並繼續獲取System.FormatException已被拋出。似乎無法弄清楚爲什麼,這讓我發瘋。我在c編程#System.FormatException已被拋出

using System; 
namespace Bank_Database 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 
      //Strings; 


      //Ints; 
      int x = 24; 
      int y = 12; 
      int Correct = 0; 

      //Loading; 
      for(int a = 0; a <= 3; a++) 
      { 
       //First Step (Step 1) 
       System.Threading.Thread.Sleep(150); 
       Console.Clear(); 
       x = 24; 
       y = 12; 

       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x++; 
       y--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x++; 
       x++; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       y++; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 


       //Secpmd Step (Step 2) 
       System.Threading.Thread.Sleep(150); 
       Console.Clear(); 

       x = x + 3; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x++; 
       y--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x++; 
       x++; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       y++; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 



       //Third Step (Step 3) 
       System.Threading.Thread.Sleep(150); 
       Console.Clear(); 

       y = y - 3; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x++; 
       y--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x++; 
       x++; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       y++; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 


       //Fourth Step (Step 4) 
       System.Threading.Thread.Sleep(150); 
       Console.Clear(); 

       x = x + 3; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x++; 
       y--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x--; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       x++; 
       x++; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 

       y++; 
       Console.SetCursorPosition (x, y); 
       Console.WriteLine ("X"); 
       } 
      //Starting Interface: 
      do { 
       Console.Clear(); 
       Console.SetCursorPosition (3, 3); 
       Console.WriteLine ("Login"); 

       Console.SetCursorPosition (3, 5); 
       Console.WriteLine ("Create New User"); 

       Console.SetCursorPosition (3, 7); 
       int Answer = Convert.ToInt32(Console.ReadLine()); 

      } while(Correct == 0); 

     } 
    } 
} 

回答

1

您可以使用int.TryParse以避免異常。

更換

int Answer = Convert.ToInt32(Console.ReadLine()); 

int Answer; 
if (!int.TryParse(Console.ReadLine(), out Answer)) 
{ 
    Console.WriteLine("Invalid Number"); 
} 
+0

,完美的工作! –

相關問題