随机化整数生成器? [英] Randomizing Integer Generator?
本文介绍了随机化整数生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在进行一个乒乓球项目,我允许用户在开始游戏之前选择他们的桨的颜色。但是,每当我点击是时,它总是会将颜色更改为红色(第一种情况),即使我反复按是。我在这做错了什么?谢谢!
I am making a pong project where I allow the user to pick the color of their paddle before starting the game. However, whenever I click "Yes", it will always change the color to red (The first case), even if I repeatedly press "Yes". What am I doing wrong here? Thanks!
Dim n As String
Dim Colors(6) As Color
Dim x As Integer
Dim Result As String
Colors(1) = Color.Red
Colors(2) = Color.Orange
Colors(3) = Color.Yellow
Colors(4) = Color.Blue
Colors(5) = Color.Green
Colors(6) = Color.Purple
Do
Result = MessageBox.Show("Would you like a new color for your paddle?", "Color", MessageBoxButtons.YesNo)
If Result = DialogResult.Yes Then
Randomize()
Select Case x = Int(1 + (Rnd() * 6))
Case x = 1
frmGame.UserPaddle.BackColor = Colors(1)
Case x = 2
frmGame.UserPaddle.BackColor = Colors(2)
Case x = 3
frmGame.UserPaddle.BackColor = Colors(3)
Case x = 4
frmGame.UserPaddle.BackColor = Colors(4)
Case x = 5
frmGame.UserPaddle.BackColor = Colors(5)
Case x = 6
frmGame.UserPaddle.BackColor = Colors(6)
End Select
ElseIf Result = DialogResult.No Then
MessageBox.Show("Enjoy the game!", "Color")
End If
Loop Until Result = DialogResult.No
MsgBox("Start the game by pressing the spacebar!")
End Sub
推荐答案
这里有什么问题?这段代码是由非程序员编写的,他可以在编程中挫败一切的目的。
例如,这个案例陈述。这不仅仅是代码太长,这是一个错误,因为 - 谁可以保证你总会有这6个案例?它应该是:
What''s wrong here? This code is written by a non-programmer, who can defeat the purpose of everything in programming.
For example, this case statement. This is not just too long code, this is a bug, because — who can guarantee that you always will have those 6 cases? It should be:
Dim index As Integer = Int(1 + (Rnd() * 6))
' I did not check it up, let's hope it's correct :-)
frmGame.UserPaddle.BackColor = Colors(index)
让我们希望你很快就能学到这些基础知识。
现在,错误是:携带随机化
退出循环。它应该只在应用程序运行时调用一个。
Let''s hope you will learn those basics very soon.
Now, the bug is: carry Randomize
out of loop. It should be called only one in the application runtime.
这篇关于随机化整数生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文