抛出了System.FormatException

时间:2015-09-20 03:50:57

标签: c#

我正在尝试转换我的整数并继续获取System.FormatException。似乎无法弄清楚为什么,这让我疯了。我在c#编程

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 个答案:

答案 0 :(得分:1)

您可以使用int.TryParse来避免例外。

替换

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

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