debugging 我尝试制作一个交互式文本游戏,但当我尝试玩家输入时,它显示(发生异常:CLR/系统格式异常)代码>>

w9apscun  于 2023-01-02  发布在  其他
关注(0)|答案(1)|浏览(108)
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继续或关闭游戏,请解释我做错了什么。谢谢

lbsnaicq

lbsnaicq1#

您有太多不需要的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();
        }
    }   
}

相关问题