一旦方法正确,我将如何重置30秒,到目前为止,在每种方法正确之后,我中止线程,然后在下一个方法中启动一个新线程,然后启动该线程,但是控制台仍然在30秒后关闭,而不是关闭重置计时器? 我以为这是秒表计时器问题?
namespace Calculator
{
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();
q2();
}
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();
}
}
static void q2() //Return type is a string as a string prompting the user will ask them to try again
{
Thread Threadq2 = new Thread(ThreadStart);
Threadq2.Start();
Console.WriteLine("2+2"); //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 == 4) //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();
}
}
}
}
答案 0 :(得分:1)
首先,您实际上并不需要这里的线程
第二,Timer
可能不是您想要的,并且您似乎可以找到一种更简便的测量时间的解决方案。
public static DateTime _starTime;
...
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.");
_starTime = DateTime.Now();
...
然后,当您要检查(即在用户回答后)时,您可以执行以下操作
var seconds = DateTime.Now.Subtract(_starTime).TotalSeconds;
If(seconds > 30)
{
// game over
}
答案 1 :(得分:0)
为什么不将timer.Restart();
放在q1 / q2方法的开头?这样,每次您调用计时器时,计时器都会重新启动
答案 2 :(得分:0)
尝试使用Timer
设置间隔为30秒。发生事件时,只需关闭控制台即可。
private static System.Timers.Timer timer = new System.Timers.Timer(2000);
static void Main(string[] args)
{
timer.Elapsed += Timer_Elapsed;
timer.Start();
// show instructions
while (true)
{
string answer = Console.ReadLine();
if (answer == "2") // check the answer somehow
{
timer.Stop(); // and restart the timer
timer.Start();
// show next question
}
}
}
private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("Too late");
timer.Elapsed -= Timer_Elapsed;
System.Threading.Thread.Sleep(2000);
Environment.Exit(0);
}