【问题标题】:Why is the program setting it to "else" when it should be on "if" here?为什么程序应该在“if”上时将其设置为“else”?
【发布时间】:2013-05-20 20:41:35
【问题描述】:

我正在开发一款游戏。

场景中有一个图片框,图片框的图像设置在一个数组中,该数组在用户在文本框中回答正确的图像名称后随机变化。

我现在已经设置了一个 Int 变量,正确增加,错误减少。

它会播放正确的特定声音和错误的特定声音。

我为什么要调试, 它可以正常工作几次,然后, 通过按钮 1 点击, 它转到 else 而不是 if。

因此,即使在文本框中设置了正确的输入,我也不会增加 Int 并播放错误的音频。这是代码:

namespace AngelinaSkriver2
{
public partial class Form1 : Form
{

    Bitmap[] bildeListe = new Bitmap[4];

    int poengInt;

    Random r = new Random();


    public Form1()
    {
        InitializeComponent();

        bildeListe[0] = Properties.Resources.ål;
        bildeListe[1] = Properties.Resources.ant;
        bildeListe[2] = Properties.Resources.apple;
        bildeListe[3] = Properties.Resources.arm;

        pictureBox1.Image = bildeListe[r.Next(0, 3)];

    }

    public void button1_Click(object sender, EventArgs e)
    {
        int tilfeldigBildet = r.Next(0, 3);
        SoundPlayer riktigLyd = new SoundPlayer("lyd/applause.wav");
        SoundPlayer feilLyd = new SoundPlayer("lyd/feil.wav");

        if (pictureBox1.Image == bildeListe[0])
        {
            if (textBox1.Text.Trim().ToLower() == "ål")
            {

                riktigLyd.Play();

                poengInt += 1;

                textBox1.Text = "";

                pictureBox1.Image = bildeListe[tilfeldigBildet];

            }
            else
            {

                feilLyd.Play();

                poengInt -= 1;

                textBox1.Text = "";
            }

            String poengString = poengInt.ToString();
            label1.Text = poengString;

        }


        if (pictureBox1.Image == bildeListe[1])
        {
            if (textBox1.Text.Trim().ToLower() == "maur")
            {

                riktigLyd.Play();

                poengInt += 1;

                textBox1.Text = "";

                pictureBox1.Image = bildeListe[tilfeldigBildet];

            }
            else {

                feilLyd.Play();

                poengInt -= 1;

                textBox1.Text = "";
            }

            String poengString = poengInt.ToString();
            label1.Text = poengString;

        }

        if (pictureBox1.Image == bildeListe[2])
        {
            if (textBox1.Text.Trim().ToLower() == "eple")
            {

                riktigLyd.Play();

                poengInt += 1;

                textBox1.Text = "";

                pictureBox1.Image = bildeListe[tilfeldigBildet];
            }
            else
            {

                feilLyd.Play();

                poengInt -= 1;

                textBox1.Text = "";
            }

            String poengString = poengInt.ToString();
            label1.Text = poengString;

        }

        if (pictureBox1.Image == bildeListe[3])
        {
            if (textBox1.Text.Trim().ToLower() == "arm")
            {

                riktigLyd.Play();

                poengInt += 1;

                textBox1.Text = "";

                pictureBox1.Image = bildeListe[tilfeldigBildet];
            }
            else
            {

                feilLyd.Play();

                poengInt -= 1;

                textBox1.Text = "";
            }

            String poengString = poengInt.ToString();
            label1.Text = poengString;

        }

    }

我似乎找不到任何应该使它不起作用的东西,我是否忽略了什么?

【问题讨论】:

    标签: arrays if-statement random picturebox


    【解决方案1】:

    无法从这段代码中说出原因。您已确保文本与调试文本框中的文本完全一致?

    但一些一般性建议:请使用 textBox1.Text.Trim().ToLower() 并进行比较。这样您就不必检查那么多不同的版本。

    如果图像在数组中,为什么单词也不在数组中?那么你只需要一个比较。只需存储图像/单词的索引即可。比较图像对象虽然可行,但也不是最好的方法。

    而不是 poengInt += -1;而是 poengInt -= 1;更好,更容易理解。

    为什么一开始就为正确和错误创建SoundPlayer的实例,然后在实际播放声音的时候再创建呢?

    还要从一开始就使用解释性变量名称,而不是 textBox1、Form1 等。从长远来看,它会变得容易得多。

    【讨论】:

    • 甚至是 poengint——还有一个测试文本的功能?
    • 是的,文字是正确的。 :) 我也会应用您建议的一些更改。
    • 如果问题显示一个苹果,我会输入“苹果”。我会显示正确的。 Apple 再次显示,我将再次输入“Apple”,但现在它不起作用。它会播放错误声音。
    猜你喜欢
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 2019-06-07
    • 2021-12-18
    相关资源
    最近更新 更多