【问题标题】:Need help making score tally for Mr and Mrs game需要帮助为 Mr and Mrs 游戏计分
【发布时间】:2014-12-04 13:50:37
【问题描述】:

我正在尝试制作一个 mr 和 mrs 风格的游戏,其中一个用户输入答案,另一个用户试图猜测另一个人说了什么。一切正常,直到比较用户的答案以查看谁是正确的部分。我不确定到底出了什么问题。非常感谢任何帮助。

Module Module1

Sub Main()
    Dim Question(4) As String
    Dim P1Answer(4) As String
    Dim P2Answer(4) As String
    Dim P1Name As String = ""
    Dim P2Name As String = ""
    Dim Count As Integer = 0
    Dim UserScore As Integer = 0
    Const ArraySize As Integer = 5

    While Count <> 4
        Question(Count) = ""
        Count = Count + 1
    End While

    Count = 0

    Question(0) = " favourite colour?"
    Question(1) = " age?"
    Question(2) = " best friends name?"
    Question(3) = " favourite food?"
    Question(4) = " first pet's name?"

    Console.WriteLine("Enter the name of player 1")
    P1Name = Console.ReadLine
    Console.WriteLine("Enter the name of player 2")
    P2Name = Console.ReadLine

    Console.WriteLine(P1Name & ". Please get ready to enter your answers.")
    Console.WriteLine("Tell " & P2Name & " to go away while you answer the questions")
    Console.WriteLine("Press enter when you are ready to begin.")
    Console.ReadLine()

    While ArraySize <> Count
        Console.WriteLine("What is your" & Question(Count))
        P1Answer(Count) = Console.ReadLine
        Count = Count + 1
    End While

    Count = 0

    Console.Clear()

    Console.WriteLine("Tell " & P2Name & " to come back now.")
    Console.WriteLine(P2Name & ". Please get ready to enter your answers.")
    Console.WriteLine("Press enter when you are ready to begin.")
    Console.ReadLine()

    While ArraySize <> Count
        Console.WriteLine("What is " & P1Name & "'s" & Question(Count))
        P2Answer(Count) = Console.ReadLine
        Count = Count + 1
    End While

    Count = 0

    **While Count <> 5
        If P1Answer(Count) = P2Answer(Count) Then
            UserScore = UserScore + 1
        Else
            UserScore = UserScore
        End If
        Question(Count) = Question(Count + 1)
    End While**

    Console.WriteLine("Your total score is " & UserScore)

    Console.ReadLine()
End Sub

结束模块

【问题讨论】:

  • 在循环开始处设置断点,然后观察代码执行;您可以使用鼠标查看每个答案数组中的内容并进行比较以确定问题。它似乎没有考虑大小写(“Blue”“blue”)并且Else也没有意义
  • 在我输入玩家 2 的答案后运行程序时,循环似乎卡住了。

标签: vb.net visual-studio-2010 visual-studio vba visual-studio-2012


【解决方案1】:

我发现您的代码存在一些问题 - 第一个最常见的问题是大写可能会导致错误的错误答案。

因此,例如,玩家 1 说她的名字是“Anne”,而玩家 2 回答“anne” - 您的代码没有考虑到这一点。

下一个问题是您实际上并没有增加 Count 变量。

按照您的编码风格,我建议这样编写循环:

Count = 0

While ArraySize <> Count
    If UCase(P1Answer(Count)) = UCase(P2Answer(Count)) Then
        UserScore = UserScore + 1
    End If
    Count = Count + 1
End While

希望这是有道理的!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2014-11-30
    • 1970-01-01
    • 2020-02-25
    • 2016-01-08
    相关资源
    最近更新 更多