简体   繁体   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();

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.我期待文本出现,然后能够选择 1 或 2 继续或关闭游戏,请解释我做错了什么。 Thank you谢谢

You have too many Console.ReadLine() that are not needed, you also declare and read decision to early.您有太多不需要的Console.ReadLine() ,您还尽早声明和阅读decision 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();
        }
    }   
}

暂无
暂无

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

相关问题 尝试解析DateTime时-mscorlib.dll中发生了'System.FormatException'类型的未处理异常 - When trying to parse DateTime - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll 发生类型为'System.FormatException'的未处理的异常 - An unhandled exception of type 'System.FormatException' occurred C#当我使用TryParse时,为什么会收到“未处理的异常:System.FormatException:输入字符串的格式不正确。” - C# Why I get “Unhandled Exception: System.FormatException: Input string was not in a correct format.” when I use TryParse? 为什么我的代码说System.FormatException:“输入字符串的格式不正确。” 当我将文本框留空并单击提交按钮时? - Why does my code say System.FormatException: 'Input string was not in a correct format.' when I leave the text box blank and click the submit button? 我得到了这个异常:System.FormatException:'输入字符串的格式不正确。' - I got this exception: System.FormatException: 'Input string was not in a correct format.' c# 如何修复:“未处理的异常:System.FormatException:输入字符串的格式不正确。” - c# How Can I Fix: “Unhandled Exception: System.FormatException: Input string was not in a correct format.” mscorlib.dll中发生了'System.FormatException'类型的未处理的异常其他信息:输入字符串的格式不正确 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format mscorlib.dll 中出现“System.FormatException”类型的异常,但未在用户代码中处理 - An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code mscorlib.dll中发生类型'System.FormatException'的异常,但未在用户代码中处理 - An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in the user code mscorlib.dll中发生了'System.FormatException'类型的未处理异常 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM