繁体   English   中英

我正在尝试制作一个交互式文本游戏,但是当我尝试播放器输入时,它说(发生异常:CLR/System.FormatException)代码>>

[英]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();

我期待文本出现,然后能够选择 1 或 2 继续或关闭游戏,请解释我做错了什么。 谢谢

您有太多不需要的Console.ReadLine() ,您还尽早声明和阅读decision 您可以尝试以下操作:

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();
        }
    }   
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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