0

让我首先向您展示我到目前为止的代码:

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?

  • 如果线程正在运行,如何终止它?(在这种情况下,“询问”线程在他们输入答案之前不会结束。)

请告诉我要改变什么。我不知道编码单词和术语,因为我几周前才开始。请尽量说清楚。

4

2 回答 2

0

要等待线程,请使用 Join()... 这将告诉线程哪个函数被调用以等待线程,直到它重新加入:

ask.Join(int);
于 2011-01-19T21:38:44.910 回答
0
      static string preanswer;
      static decimal answer = 0;

      static void Main() 
      {
       int score = 0;
       //string preanswer;
       //decimal answer = 0;
...

等等

于 2011-01-19T21:33:00.113 回答