简体   繁体   中英

I'm trying to make an interactive text game like but when I try the player input it says (Exception has occurred: CLR/System.FormatException) code>>

using System;

namespace Coding_basics
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = " To Infinity";
            Console.ForegroundColor = ConsoleColor.Green;
           int decision  = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Welcome");
            Console.ReadLine();
            Console.WriteLine("You have entered the program, press 1 to proceed or press 2 to leave");
            Console.ReadLine();
            if (decision == 1 )  { Console.WriteLine("Good luck...");
            } 

            if (decision == 2) { Environment.Exit(0);

            }
            Console.ReadKey();

I was expecting the text to come up and then be able to choose 1 or 2 to continue or close the game, please explain what I'm doing wrong. Thank you

You have too many Console.ReadLine() that are not needed, you also declare and read decision to early. You can try the below:

using System;

namespace Coding_basics
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "To Infinity";
            Console.ForegroundColor = ConsoleColor.Green;

            Console.WriteLine("Welcome");
            Console.WriteLine("You have entered the program, press 1 to proceed or press 2 to leave");
            
            int decision  = Convert.ToInt32(Console.ReadLine());
            
            if (decision == 1 )
            {
                Console.WriteLine("Good luck...");
            }
            if (decision == 2)
            {
                Environment.Exit(0);
            }
            
            Console.ReadKey();
        }
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM