私は、これまであなたに私のコードを示すことから始めましょう:スレッド:スレッドの変数へのアクセスと終了
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);
}
}
}
だから、私がしようとすると、このコードをコンパイルするとき、私はpreanswerと答えをCS0103エラーが発生します"プロンプト"メソッド。
これは、3つの質問につながる:
まさに私がpreanswerを作成し、「プロンプト」メソッドにアクセス答えるようにしなければならないのですか?
スコア変数に1を正しく追加しましたか?
スレッドが実行中の場合、どのように終了できますか? (この場合、「ask」スレッドは回答を入力するまで終了しません)。
変更する内容を教えてください。私はちょうど数週間前に始まったので、言葉や用語のコーディングを知らない。可能な限り明確にしてください。
待機 - メソッドの外に変数を設定したいですか?私はそれが可能であったことを知りませんでした... –
はい、そうでなければあなたのスレッドメソッドには見えません。 –
ああ、それは働いた!あなたは "ask"スレッドを終了することについて何か知っていますか? –