让我首先向您展示我到目前为止的代码:
using System;
using System.Threading;
class MathQuiz
{
static void Main()
{
int score = 0;
string preanswer;
decimal answer = 0;
Console.WriteLine("Welcome to Project5, a MathQuiz project.");
Console.WriteLine("You will be asked 10 questions, and will have 30 seconds to read and answer each one.");
Console.WriteLine("Press any key to begin.");
Console.ReadKey(true);
Console.WriteLine("What is 2 + 2?");
Thread ask = new Thread (new ThreadStart (MathQuiz.prompt));
ask.Start();
Thread.Sleep(3000);
//This is where I want to end the thread if it isn't already done.
if (answer == 4)
{
score = score+1; //Here's where I don't know if my adding is correct.
}
Console.WriteLine("Press any key to move on to the next question!");
Console.ReadKey(true);
}
static void prompt()
{
preanswer = (Console.ReadLine());
if (!decimal.TryParse(preanswer, out answer))
{
Console.WriteLine("That wasn't even a number or decimal!");
}
else
{
answer = decimal.Parse(preanswer);
}
}
}
因此,当我尝试编译此代码时,我在“提示”方法中得到预回答和回答的 CS0103 错误。
这导致了3个问题:
我到底需要做什么才能使“提示”方法可以访问预回答和回答?
我是否在 score 变量上正确添加了 1?
如果线程正在运行,如何终止它?(在这种情况下,“询问”线程在他们输入答案之前不会结束。)
请告诉我要改变什么。我不知道编码单词和术语,因为我几周前才开始。请尽量说清楚。