系统.已抛出FormatException

本文关键字:FormatException 系统 | 更新日期: 2023-09-27 18:13:27

我正在尝试转换我的整数并不断获得系统。已抛出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);
      }
   }
}

系统.已抛出FormatException

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

代替

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

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