[英]When trying to parse DateTime - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
[英]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.