我创建了一个 30 秒的计时器,我将为每个方法使用。我如何显示足够长的消息让他们阅读它,然后终止程序?
namespace Calculator{
class Program
{
static void Main(string[] args)
{
System.Timers.Timer timer = new System.Timers.Timer(5000);
Console.WriteLine("INSTRUCTIONS - You have 30 seconds to answer each question correctly, once you get the question right the next question will appear," +
"if you get a question wrong the console will display INCORRECT and you will have until the end of the 30 seconds to answer it correctly.");
//These are the instructions
timer.Start();
q1();
timer.Stop();
}
static string q1() //Return type is a string as a string prompting the user will ask them to try again
{
Console.WriteLine("1+1"); //This is the question
int answer = Convert.ToInt32(Console.ReadLine());// Can't apply int to a readline, so convert the useres input to an int so you can apply an int variable
if (answer == 2) //If the users input is equal to 2
{
Console.WriteLine("Correct");//Tells the user that they are correct
}
return "Incorrect, try again";//Promts the user to trya again
}
}
}
你可以这样做:
Console.WriteLine("Press any key to continue");
Console.ReadLine();
控制台将等到按下任何键继续(如果是最后一件事,则退出程序(
class Program
{
static ThreadStart ThreadStart = new ThreadStart(Counter);
static Thread Thread = new Thread(ThreadStart)
{
Priority = ThreadPriority.Highest
};
static void Main(string[] args)
{
Console.WriteLine("INSTRUCTIONS - You have 30 seconds to answer each question correctly, once you get the question right the next question will appear," +
"if you get a question wrong the console will display INCORRECT and you will have until the end of the 30 seconds to answer it correctly.");
//These are the instructions
Thread.Start();
q1();
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
static Stopwatch timer = Stopwatch.StartNew();
static void Counter()
{
if(timer.ElapsedMilliseconds < 30000)
{
Thread.Sleep(1000);
Counter();
}
else
{
Console.WriteLine("Too late");
Environment.Exit(0);
}
}
static void q1() //Return type is a string as a string prompting the user will ask them to try again
{
Console.WriteLine("1+1"); //This is the question
int answer = Convert.ToInt32(Console.ReadLine());// Can't apply int to a readline, so convert the useres input to an int so you can apply an int variable
if (answer == 2) //If the users input is equal to 2
{
Console.WriteLine("Correct");//Tells the user that they are correct
Thread.Abort();
}
else
{
Console.WriteLine("Try again");
q1();
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("INSTRUCTIONS - You have 30 seconds to answer each question correctly, once you get the question right the next question will appear," +
"if you get a question wrong the console will display INCORRECT and you will have until the end of the 30 seconds to answer it correctly.");
//These are the instructions
q1();
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
static Stopwatch timer = Stopwatch.StartNew();
static void q1() //Return type is a string as a string prompting the user will ask them to try again
{
Console.WriteLine("1+1"); //This is the question
int answer = Convert.ToInt32(Console.ReadLine());// Can't apply int to a readline, so convert the useres input to an int so you can apply an int variable
if (timer.ElapsedMilliseconds > 30000)
{
Console.WriteLine("It took you too much time");
}
if (answer == 2) //If the users input is equal to 2
{
Console.WriteLine("Correct");//Tells the user that they are correct
}
else
{
Console.WriteLine("Try again");
q1();
}
}
}
您的方法也不需要任何回报,您没有在任何地方使用它。