C# 检查两个键是否匹配不是';不正常工作

C# 检查两个键是否匹配不是';不正常工作,c#,visual-studio-2008,combobox,C#,Visual Studio 2008,Combobox,这个应用程序的工作原理就像你在玩彩票一样,你从一个组合框中选择5个数字,点击一个按钮生成5个关键数字,然后按下另一个按钮检查结果(在你在下面的文本框中引入奖品monei后,也称为“prémio”) 执行检查的按钮是突出显示的按钮Verificar Prémio 代码如下: private void button5_Click(object sender, EventArgs e) { if (textBox1.Text != "" && textBox1.Text!=

这个应用程序的工作原理就像你在玩彩票一样,你从一个组合框中选择5个数字,点击一个按钮生成5个关键数字,然后按下另一个按钮检查结果(在你在下面的文本框中引入奖品monei后,也称为“prémio”)

执行检查的按钮是突出显示的按钮Verificar Prémio

代码如下:

private void button5_Click(object sender, EventArgs e)
{

    if (textBox1.Text != "" && textBox1.Text!="Prémio em €")
    {
        int contador = 0;

        for (int i = 1; i <= 5; i++)
        {
            string posicao = i.ToString();

            for (int c = 1; c <= 5; c++)
            {
                string poschave = c.ToString();
                if (listBox1.Items.IndexOf(posicao) == 
                    listBox2.Items.IndexOf(poschave))
                {
                    contador = contador + 1;

                }

            }

            i = int.Parse(posicao);

            double valor;
            double premio = double.Parse(textBox1.Text);

            if (contador == 5)
            {
                MessageBox.Show(" Parabens ganhou o 1º premio acertou 5 números 
                    o seu prémio é de " + premio + "€");
            }
            else
            {
                if (contador == 4)
                {
                    valor = premio * 0.75;
                    MessageBox.Show(" Acertou 4 numeros o seu premio é: " + 
                       valor + "€");
                }
                else
                {
                    if (contador == 3)
                    {
                        valor = premio * 0.5;
                        MessageBox.Show("Acertou 3 numeros o seu premio é: " + 
                           valor + "€");
                    }
                    else
                        if (contador <= 2)
                        {
                            MessageBox.Show(" Infelizmente nao ganhou, 
                               nada tente outra vez");
                        }
                    }

                }
            }
        }
    }
错误不再显示,但仍然无法正常工作,我猜程序正在检查它们是否匹配,然后得到结果,因此它总是连续5次显示“你一无所获”

如何解决此问题?

我不懂西班牙语(?),因此很难理解您的代码(请使用英语变量名,即使您有本地化的UI)

但是,问题的一个原因可能是这条线路:

listBox1.Items.IndexOf(posicao) == listBox2.Items.IndexOf(poschave)
如果在各自的列表框中未找到
posicao
poschave
,则将返回-1,表达式将为true(即
contador
将递增)。我猜这不是你想要的行为

如果您想检查左侧列表框中的项目在右侧是否也可用,则可以执行以下操作:

void Button_Click(object sender, EventArgs e)
{ 
    if (textBox1.Text == "" || textBox1.Text == "Prémio em €")
        return;
    int numMatches = 0;
    foreach (var item in listBox1.Items)
    {
        if (listBox2.Items.Contains(item))
            numMatches++;
    }
    // numMatches will now contain the number of elements in the left 
    // listbox that also exist in the right listbox
    if (numMatches > 2)
    {
        double premio = Double.Parse(textBox1.Text);
        double prize = 0;
        if (numMatches == 5)
          prize = premio * 1.0;
        if (numMatches == 4)
          prize = premio * 0.75;
        else 
          prize = premio * 0.5;
        // Use string.Format() instead to get fancier formatting of numbers
        Messagebox.Show ("Sweet, you got " + numMatches + " out of 5 correct numbers. Your prize is " + prize + "€");
    }
    else
    {
        MessageBox.Show("Sorry, not enough matches - you win nothing!");
    }
}
编辑:
之所以会出现5个消息框,是因为在循环5次的for循环中调用了
Messagebox.Show()
。我已经更新了上面的代码示例,以执行我认为您希望按钮回调执行的操作

您的源代码太复杂了,您有两个循环,一个是整数>字符串,后面是字符串>整数。。。试试这个:

int count = 0;
for (int i = 0; i < 5; i++)
{
    if (listBox2.Items.IndexOf(listBox1.Items[i]) > 0)
    {
        count++;
    }
}
// count is 0 - 5
int count=0;
对于(int i=0;i<5;i++)
{
if(listBox2.Items.IndexOf(listBox1.Items[i])>0)
{
计数++;
}
}
//计数是0-5

如果左列表框在右列表框中,则只检查左列表框中的5个数字。

检查索引“如果(listBox1.Items.IndexOf(posicao)==listBox2.Items.IndexOf(poschave))”是否应该检查值?另外,为什么要检查行“i=int.Parse(posicao);”?posiaco已经是i的字符串化版本,在for循环中操作循环变量通常不是一个好主意。你可以考虑用一个Switter case Exchange HM替换嵌套的IFS,我该怎么做呢?对不起,我刚开始编程不久,这是我第一次做类似的事情this@Madcowe:我不是100%确定您希望代码做什么,但我添加了一个示例,我认为这就是您想要的want@isak正如我在上面所评论的,这段代码也没有给出任何错误,但我认为程序会一直运行到结束,然后重复,因此说5次“你没赢”@isak非常感谢,我会在测试后立即回复你,我将其替换为:“for(int c=1;c@Madcowe:试试方括号:
listBox1.Items[I]
@IsakSavo该错误不再显示,但仍然无法正常工作,我猜程序正在检查它们是否匹配,然后得到结果,因此它总是连续5次显示“你一无所获”…@Isak谢谢,忘了重新检查语法
void Button_Click(object sender, EventArgs e)
{ 
    if (textBox1.Text == "" || textBox1.Text == "Prémio em €")
        return;
    int numMatches = 0;
    foreach (var item in listBox1.Items)
    {
        if (listBox2.Items.Contains(item))
            numMatches++;
    }
    // numMatches will now contain the number of elements in the left 
    // listbox that also exist in the right listbox
    if (numMatches > 2)
    {
        double premio = Double.Parse(textBox1.Text);
        double prize = 0;
        if (numMatches == 5)
          prize = premio * 1.0;
        if (numMatches == 4)
          prize = premio * 0.75;
        else 
          prize = premio * 0.5;
        // Use string.Format() instead to get fancier formatting of numbers
        Messagebox.Show ("Sweet, you got " + numMatches + " out of 5 correct numbers. Your prize is " + prize + "€");
    }
    else
    {
        MessageBox.Show("Sorry, not enough matches - you win nothing!");
    }
}
int count = 0;
for (int i = 0; i < 5; i++)
{
    if (listBox2.Items.IndexOf(listBox1.Items[i]) > 0)
    {
        count++;
    }
}
// count is 0 - 5