[英]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.