【问题标题】:XNA quiz game answer checkerXNA问答游戏答案检查器
【发布时间】: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


    【解决方案1】:

    您的答案检查也可能存在问题,但是当您阅读文件时肯定会遇到前导/尾随间距问题。你应该Trim()你的分裂部分。所以这可能只是您需要的答案的一半。

    你的零件数组目前看起来像这样。

    parts[0] = "question 1"
    parts[1] = " TRUE"
    parts[2] = " image1"
    

    将你的代码换成这个会有所帮助。

    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].Trim();
    
      string correctAnswer = parts[1].Trim()
      if (correctAnswer  == "TRUE")
      {
        datasoalnya.jawaban = true;
      }
      else if (correctAnswer  == "FALSE")
      {
        datasoalnya.jawaban = false;
      }
    
      datasoalnya.tempatImage = parts[2].Trim();
      soalList.Add(datasoalnya);
    }
    

    作为旁注,我建议在将字符串转换为布尔值时使用bool.TryParse()。它将使您的代码更清晰,错误更少。这就是尝试解析后的样子。

    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].Trim();
    
      bool isCorrectAnswer;
      if(bool.TryParse(parts[1], out isCorrectAnswer)
      {
        datasoalnya.jawaban = isCorrect;
      }
      else
      {
        // unable to parse, add logic to recover or error out
      }
    
      datasoalnya.tempatImage = parts[2].Trim();
      soalList.Add(datasoalnya);
    }
    

    【讨论】:

    • 以及我如何将 bool.TryParse() 放入我的代码中......?还是不太明白……
    【解决方案2】:
    if (datasoalnya.jawaban = true)
    ...
    else if (datasoalnya.jawaban = false)
    

    您可能希望将 = 更改为 ==,这也被编译器建议为警告,并在 IDE 中带有绿色下划线。

    【讨论】:

    • 是的,就是这样。我正要发布它。接受 CodeCaster 的回答和我的回答,你就可以解决问题了。
    • 我也已经尝试过了。当我使用“=”时,程序只做datasoal.jawaban的真条件,而不是假条件。但是当我使用“==”时,它反转了,程序处理的错误条件
    • 如果你仍然有这个问题......你需要去阅读编程书籍介绍。
    【解决方案3】:

    试试这个:

    bool? button = null;
    if (
       (mouseState.LeftButton == ButtonState.Pressed && 
        prevMouseState.LeftButton == ButtonState.Released) &&
        (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(correctRectangle)))
            button = true;
    else
    if (
       (mouseState.LeftButton == ButtonState.Pressed && 
        prevMouseState.LeftButton == ButtonState.Released) &&
        (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(falseRectangle)))
            button = false;
    if (button != null)
    {
        ((Game1)Game).SoundBank.PlayCue("1_btn_click02");
        if (datasoalnya.jawaban == button.Value) score += 100;
        noSoal++;
        if (noSoal == 11)
        {
            mouseDiBenar = true;
            noSoal = 1;
            if (datasoalnya.jawaban == button.Value) score = 0;
        }
    }
    

    【讨论】:

    • 我已经试过了。它仍然无法正常工作。它仍然没有使用文本文件中的答案数据检查输入...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多