【发布时间】:2012-01-01 00:21:17
【问题描述】:
现在我正在创建一个真假问答游戏。 问题、问题的图像和答案存储在文本文件中。所以主程序只显示来自该文本文件的问题和图像。当玩家输入答案时,通过点击 True 或 False 按钮,程序将在文本文件中检查该问题的答案。
文本文件是这样的:
question 1, TRUE, image1
question 2, FALSE, image2
question 3, FALSE, image3
question 4, TRUE, image4
question 5, TRUE, image5
question 6, TRUE, image6
question 7, FALSE, image7
question 8, TRUE, image8
question 9, FALSE, image9
question 10, FALSE, image10
这就是我从文本文件中读取数据的方式:
StreamReader sr = new StreamReader("data.txt");
string line;
while ((line = sr.ReadLine()) != null)
{
string[] parts = line.Split(',');
Soal datasoalnya = new Soal();
datasoalnya.soal = parts[0];
if (parts[1] == "TRUE")
{
datasoalnya.jawaban = true;
}
else if (parts[1] == "FALSE")
{
datasoalnya.jawaban = false;
}
datasoalnya.tempatImage = parts[2];
soalList.Add(datasoalnya);
}
这就是我检查答案的方式:
if ((mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released) &&
(new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(correctRectangle)))
{
((Game1)Game).SoundBank.PlayCue("1_btn_click02");
if (datasoalnya.jawaban = true)
{
score += 100;
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
score = 0;
}
}
else if (datasoalnya.jawaban = false)
{
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
}
}
}
else if ((mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released) &&
(new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(falseRectangle)))
{
((Game1)Game).SoundBank.PlayCue("1_btn_click02");
if (datasoalnya.jawaban = true)
{
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
score = 0;
}
}
else if (datasoalnya.jawaban = false)
{
score += 100;
noSoal++;
if (noSoal == 11)
{
mouseDiBenar = true;
noSoal = 1;
}
}
}
问题是每当玩家输入答案时,程序都不会检查输入的条件,无论是真还是假。就像每当玩家点击正确按钮时,分数总是乘以 100,无论它在文本文件中是真是假。
我怎样才能让程序检查答案...?
【问题讨论】:
标签: c# if-statement xna streamreader string-parsing