簡體   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