此应用程序的工作方式就像您正在玩彩票一样,您从组合框中选择5个数字,单击一个按钮以生成5个关键数字,然后按下另一个按钮以检查结果(在您引入奖金后,下面的文本框,AKA“prémio”)。
执行检查的按钮是突出显示的VerificarPré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");
}
}
}
}
}
}
无论我做什么,它总是显示messageBox说我得到了所有5个正确...
编辑:
listBox1是左边的那个,(3,9,17,20,10),你从组合框中选择它们,当你点击“Apostar”时,它会被加到它上面。
listBox2是右侧的框。
EDIT2: 通过替换
for (int c = 1; c <= 5; c++)
{
string poschave = c.ToString();
if (listBox1.Items.IndexOf(posicao) == listBox2.Items.IndexOf(poschave))
{
contador = contador + 1;
}
}
与
foreach (var item in listBox1.Items)
{
// Convert it to string to avoid object reference comparison. Not 100%
// sure if this is needed
string value = item.ToString();
if (listBox2.Items.Contains(value))
contador++;
}
错误不再显示但是它仍然无法正常工作,我的猜测是程序正在检查它们是否匹配,然后得到结果,因此它总是连续5次显示“你一无所获”... < / p>
我该如何解决这个问题?
答案 0 :(得分:2)
我不懂西班牙语(?)所以很难理解你的代码(请使用英文变量名,即使你有本地化的UI)
然而,问题的一个原因可能是这一行:
listBox1.Items.IndexOf(posicao) == listBox2.Items.IndexOf(poschave)
如果在各自的列表框中找不到posicao
或poschave
,则返回-1并且表达式为真(即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个消息框的原因是你在for循环中调用Messagebox.Show()
循环五次。我已经更新了上面的代码示例,以执行我认为您希望按钮回调执行的操作
答案 1 :(得分:2)
你的来源太复杂了,你有两个循环,一个整数&gt; string后跟字符串&gt;整数...试试这个:
int count = 0;
for (int i = 0; i < 5; i++)
{
if (listBox2.Items.IndexOf(listBox1.Items[i]) > 0)
{
count++;
}
}
// count is 0 - 5
如果它在右侧ListBox中,您只检查左侧ListBox的5个数字。