计算用户响应

时间:2014-11-18 15:27:59

标签: .net vb.net

我的一些代码遇到了一些麻烦。我只使用Visual Basic大约1个半月的时间用于大学工作,而且大部分时间它都进展顺利,但是我来到了一堵砖墙而且我是#39 ; d喜欢一些帮助或建设性的批评。

我被赋予了以下任务:

  

一个小程序,用于演示如何使用“If ... Then ...”或“Case”结构进行选择的对象,事件处理程序和对象属性的使用。编写一个程序,询问五个问题,答案必须是“是”或“否”。然后,程序必须将Yes答案的数量和No答案的数量相加。如果“是”答案的数量超过“无答案”的数量,则显示“乐观主义者”的消息。或者显示'Pessimist'。 (你可以决定问题)

所以,我需要查看多少"是"和"不"答案我已经得到了,到目前为止,我的代码是:

Public Class Form1

Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click

    Select Case MsgBox("Does your outlook affect your motivation?", MsgBoxStyle.YesNo)
        Case MsgBoxResult.Yes
        Case MsgBoxResult.No

    End Select

    Select Case MsgBox("Do you have trust and faith in people?", MsgBoxStyle.YesNo)
        Case MsgBoxResult.Yes
        Case MsgBoxResult.No

    End Select

    Select Case MsgBox("Do you expect the best?", MsgBoxStyle.YesNo)
        Case MsgBoxResult.Yes
        Case MsgBoxResult.No

    End Select

    Select Case MsgBox("Do good things happen to you?", MsgBoxStyle.YesNo)
        Case MsgBoxResult.Yes
        Case MsgBoxResult.No

    End Select

    Select Case MsgBox("Are you optimistic about your future?", MsgBoxStyle.YesNo)
        Case MsgBoxResult.Yes
        Case MsgBoxResult.No

    End Select

    If MsgBoxResult.Yes > 3 Then
        MessageBox.Show("You are optimistic.")

    ElseIf MsgBoxResult.Yes < 3 Then
        MessageBox.Show("You are pessimistic.")

    End If
End Sub End Class    

考虑到我只是在短时间内编码,我只需要非常简单的修复或任何可以提供更多帮助的内容。

我的问题是,无论有多少&#34;否&#34;答案我输入,我总是回来&#34;你很乐观&#34;。如果我不清楚我在寻求帮助,请发表评论,谢谢!

2 个答案:

答案 0 :(得分:0)

MsgBoxResult.Yes的值是6.它是一个常数 - 它永远不会改变 - 所以(具有讽刺意味的是)你总是会在“你是乐观的”块中结束。

每次用户点击“是”时,您需要在函数内声明一个变量increment

答案 1 :(得分:-1)

你忘记了这部分&#34;然后程序必须加上肯定答案的数量和无答案的数量。&#34;。

您需要创建一个计算是的数量的变量。

Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click

    Dim yesCount As Integer = 0

    Select Case MsgBox("Does your outlook affect your motivation?", MsgBoxStyle.YesNo)
        Case MsgBoxResult.Yes
            yesCount += 1
        Case MsgBoxResult.No

    End Select

    Select Case MsgBox("Do you have trust and faith in people?", MsgBoxStyle.YesNo)
        Case MsgBoxResult.Yes
            yesCount += 1
        Case MsgBoxResult.No

    End Select

然后该变量应该是结果。您应该在您的情况下使用Else,如果是的数字等于no的数量(中性?),您的代码中可能会出现问题。

If yesCount > 3 Then
    MessageBox.Show("You are optimistic.")

Else
    MessageBox.Show("You are pessimistic.")

End If