2012-04-06 75 views
-2

我在Windows上运行的程序形成的Visual Basic和IM试图使多个计数器在VB.NET使用条件语句来算

这里是我的代码:

If txtAnswer.Text = nMathSum Then 
     nCount = nCount + 1 
     lblCorrect.Text = nCount 
    ElseIf txtAnswer.Text <> nMathSum Then 
     nIount = nIount + 1 
     lblIncorrect.Text = nIount 
    End If 

    If txtAnswer.Text = nMathDiff Then 
     nCount = nCount + 1 
     lblCorrect.Text = nCount 
    ElseIf txtAnswer.Text <> nMathDiff Then 
     nIcount = nIcount + 1 
     lblIncorrect.Text = nIout 
    End If 

它想看看有多少次我回答正确和不正确

柜台的总和工作正常,但差异的计数器有问题。 当我输入正确的答案时,它会转到错误的标签。

+5

nIount和ncount?当您选择怪异标识符名称时会发生这种情况。帮助您编写无bug代码的不重要的代码是GoodAnswerCount和BadAnswerCount。 – 2012-04-06 16:46:55

+0

这就是它:nIount,ncount或nIout? Hans Passant是正确的:使用更好的标识符名称! – mjv 2012-04-06 16:48:46

回答

0

txtAnswer不太可能匹配总和和差异。所以,在你的代码中,你至少会有一个不正确的。

如果txtAnswer应该匹配总和或差异,您是否有一些方法?如果是,请在检查答案前检查。

EDIT(解释一下我的意思):喜欢的东西

If operation = "+" Then 
    If txtAnswer.Text = nMathSum Then 
     nCount = nCount + 1 
     lblCorrect.Text = nCount 
    ElseIf txtAnswer.Text <> nMathSum Then 
     nIcount = nIcount + 1  ' corrected this line to use nIcount 
     lblIncorrect.Text = nIcount ' corrected this line to use nIcount 
    End If 
Else 
    If txtAnswer.Text = nMathDiff Then 
     nCount = nCount + 1 
     lblCorrect.Text = nCount 
    ElseIf txtAnswer.Text <> nMathDiff Then 
     nIcount = nIcount + 1 
     lblIncorrect.Text = nIcount ' corrected this line too 
    End If 
End If 

如果操作是设置为“+”或变“ - ”这取决于用户是否应该提供总和或差异。

+0

我想我没有办法检查总和和差异。你能告诉我怎么做。我真的需要完成这个程序 – pxtoxp 2012-04-06 16:50:34

+0

如果你没有办法检查,没有办法来帮助你。用户如何知道他们是否提供了总额或差额?无论如何,使用相同的东西来确定要检查哪个答案。 – 2012-04-06 16:53:09

+0

这是你的意思是通过检查的内容: 如果txtAnswer.Text = nMathSum然后 MessageBox.Show( “正确的,美好的工作”, “你的回答是”,MessageBoxButtons.OK,MessageBoxIcon.Asterisk) elseif的txtAnswer.Text < > nMathSum然后 MessageBox.Show(“不正确,再试一次”,“你的答案是”,MessageBoxButtons.OK,MessageBoxIcon.Error) End If End If – pxtoxp 2012-04-06 16:54:16

0

您在seconf Elseif 另外,如最后一行nIoutnIountnIcount拼写错误。将其修正为:

ElseIf txtAnswer.Text <> nMathDiff Then 
    nIount = nIount + 1 
    lblIncorrect.Text = nIount 
End If 

这是假定第一次出现(nIount)是正确的拼写。

+0

VB.Net不区分大小写。这必须是在问题中键入的代码,而不是复制和粘贴。 IDE将自动更正标识符名称。 – 2012-04-09 15:17:55

+0

这不是区分大小写。在问题的代码中有'nIount','nIcount'和'nIout',很可能只有一个(最多两个)是正确的。 – Attila 2012-04-09 18:47:19

+0

你是对的,我错过了。 – 2012-04-10 15:18:34