vb.net 随机数猜谜游戏VB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26188503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:19:26  来源:igfitidea点击:

Random number guessing game VB

vb.net

提问by R4g323

OBJECTIVE:

客观的:

This is a guessing game. The program is to generate a random number between 1 and 500. The user is to guess the number.

这是一个猜谜游戏。该程序是生成一个 1 到 500 之间的随机数。用户要猜测这个数字。

The form should include a START button, a listbox to hold all valid guesses, and a label displaying the answer.

该表单应包括一个 START 按钮、一个包含所有有效猜测的列表框以及一个显示答案的标签。

Upon clicking the START button, the user will enter a number in response to an InputBox().

单击 START 按钮后,用户将输入一个数字以响应 InputBox()。

If the user's guess is invalid (not numeric, not a whole number, out of range), display an appropriate message.

如果用户的猜测无效(不是数字,不是整数,超出范围),则显示适当的消息。

If the guess is valid but is not the correct number (high or low), display an appropriate message.

如果猜测有效但不是正确的数字(高或低),则显示适当的消息。

image high quess dialog boximage low quess dialog box

image high quess dialog boximage low quess dialog box

Every time the user guesses a valid numeric guess within range, add the guess to the listbox on the form. Allow the guesses to be shown in multiple columns in the listbox.

每次用户猜测范围内的有效数字猜测时,将猜测添加到表单上的列表框。允许在列表框中的多列中显示猜测。

If the user successfully guesses the number, display an appropriate message. Include how many guesses they took. Count only valid (in range, integral) numeric guesses as a guess.

如果用户成功猜出数字,则显示相应的消息。包括他们进行了多少次猜测。仅将有效(在范围内,整数)数字猜测作为猜测计数。

Allow the user to quit the game by entering Quit in response to the input box. If the user gives up, tell them the correct number

允许用户通过响应输入框输入 Quit 来退出游戏。如果用户放弃,告诉他们正确的号码

Important: Display the random number in a label immediately after generating it so that I (and you) know what the number is while I am (and you are) testing it. You can obviously take it out if you really want to play the game.

重要提示:生成随机数后立即在标签中显示随机数,以便我(和您)在我(和您)测试时知道该数字是什么。如果您真的想玩游戏,显然可以将其取出。

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    'Declare Variables
    Dim strGuess As String
    Dim random As New Random
    Dim answer As Integer

    'Start with empty Boxes
    lstGuesses.Items.Clear()


    answer = random.Next(1, 500)
    lblAnswer.Text = CStr(answer)

    Do
        Try

            strGuess = InputBox("Enter a numeric integer between 1 and 500. , Enter 'quit' to Quit.", "Guessing Game")
            lstGuesses.Items.Add(strGuess)

            If strGuess = CStr("quit") Then
                MessageBox.Show("The number was " & answer & ". Click Start Game to play again.")
                Exit Do
            End If

            If CInt(strGuess) < CInt(1) Then
                MessageBox.Show("Invalid Guess. Enter an Integer Number between 1 and 500!")

            End If

            If CInt(strGuess) > CInt(500) Then
                MessageBox.Show("Invalid Guess. Enter an Integer Number between 1 and 500!")

            End If

            If CInt(strGuess) = CInt(answer) Then
                MessageBox.Show("Got it! You guessed " & lstGuesses.Items.Add(strGuess) & " times!")
            End If

            If CInt(strGuess) > CInt(answer) And CInt(strGuess) <= CInt(500) Then
                MessageBox.Show("Guess is High")
            End If

            If CInt(strGuess) < CInt(answer) And CInt(strGuess) >= CInt(1) Then
                MessageBox.Show("Guess is Low")
            End If

            If lstGuesses.Items.Contains("quit") = True Then
                MessageBox.Show("The number was " & answer & ". Click Start Game to play again.")

            End If

        Catch ex As InvalidCastException
            'Make user guess
            MessageBox.Show("Invlid Guess. Enter a numeric integer between 1 and 500!")
        End Try

    Loop While CInt(strGuess) <> answer

End Sub

MY PROBLEM: I have been trying everything and this is the best i have made yet. This homework is due in 4 hours so any help will be appreciated. I am suppose to type the word "quit" to end the game. But also give an error message when something other than letters are typed. Every time I type a letter and press enter, it gives the warning that i set but then it crashes. It is not supposed to crash. It says the crash is from: Loop While CInt(strGuess) <> answer and the problem is because of an InvalidCastException and says convertion from string to type integer is not valid. I tried doing TRYPARSE but still the same problem. Can anyone tell me how to let me type the word "quit" into the box so that it quits the game, but doesn't make it crash.

我的问题:我一直在尝试一切,这是我做过的最好的。这项作业将在 4 小时内完成,因此我们将不胜感激。我想输入“退出”这个词来结束游戏。但在键入字母以外的其他内容时也会给出错误消息。每次我输入一个字母并按回车键时,它都会给出我设置的警告,但随后它崩溃了。它不应该崩溃。它说崩溃来自:Loop While CInt(strGuess) <> 答案,问题是由于 InvalidCastException 并表示从字符串到整数类型的转换无效。我尝试做 TRYPARSE 但仍然是同样的问题。谁能告诉我如何让我在框中键入“退出”一词,以便它退出游戏,但不会使其崩溃。

回答by Creator

Maybe this is not the best way but i think it will help.

也许这不是最好的方法,但我认为它会有所帮助。

What you need to do is Make a new if statement at the top just after the user entered something in the inputbox.

您需要做的是在用户在输入框中输入内容后在顶部创建一个新的 if 语句。

            If IsNumeric(strGuess) Or strGuess = CStr("quit") Then
            Else
                MessageBox.Show("Only Numbers")
                GoTo Line1
            End If

And here you check "If IsNumeric(strGuess) Or strGuess = CStr("quit") Then"
if yes do nothing
If No show a msgbox and use a goto "GoTo Line1" "so above your do you add Line1:"

在这里你检查“If IsNumeric(strGuess) Or strGuess = CStr("quit") Then”
如果是,什么都不做
If No 显示一个msgbox并使用goto“GoTo Line1”“所以在你上面添加Line1:”

If somebody types something like "a" he will go to line1 "so start the do" and the user needs to enter something new in the inputbox.

如果有人输入诸如“a”之类的内容,他将转到第 1 行“开始执行”,并且用户需要在输入框中输入新内容。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

'Declare Variables
    Dim strGuess As String
    Dim random As New Random
    Dim answer As Integer

    'Start with empty Boxes
    lstGuesses.Items.Clear()


    answer = random.Next(1, 500)
    lblAnswer.Text = CStr(answer)

Line1:
    Do
        Try

            strGuess = InputBox("Enter a numeric integer between 1 and 500. , Enter 'quit' to Quit.", "Guessing Game")
            lstGuesses.Items.Add(strGuess)

            If IsNumeric(strGuess) Or strGuess = CStr("quit") Then
            Else
                MessageBox.Show("Only Numbers")
                GoTo Line1
            End If

            If strGuess = CStr("quit") Then
                MessageBox.Show("The number was " & answer & ". Click Start Game to play again.")
                Exit Do
            End If

            If CInt(strGuess) < CInt(1) Then
                MessageBox.Show("Invalid Guess. Enter an Integer Number between 1 and 500!")

            End If

            If CInt(strGuess) > CInt(500) Then
                MessageBox.Show("Invalid Guess. Enter an Integer Number between 1 and 500!")

            End If

            If CInt(strGuess) = CInt(answer) Then
                MessageBox.Show("Got it! You guessed " & lstGuesses.Items.Add(strGuess) & " times!")
            End If

            If CInt(strGuess) > CInt(answer) And CInt(strGuess) <= CInt(500) Then
                MessageBox.Show("Guess is High")
            End If

            If CInt(strGuess) < CInt(answer) And CInt(strGuess) >= CInt(1) Then
                MessageBox.Show("Guess is Low")
            End If

            If lstGuesses.Items.Contains("quit") = True Then
                MessageBox.Show("The number was " & answer & ". Click Start Game to play again.")

            End If

        Catch ex As InvalidCastException
            'Make user guess
            MessageBox.Show("Invlid Guess. Enter a numeric integer between 1 and 500!")
        End Try

    Loop While CInt(strGuess) <> answer
End Sub