略阳火车站到汽车站:vb.net 从3个红球、5个白球、6个绿球中任意取出8个球,且其中必须有白球,试利用循环结构语句编写。

来源:百度文库 编辑:神马品牌网 时间:2024/05/05 10:43:49
用VB做```谁可以帮帮我``
我是新手````什么也不懂``

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents txtResult As System.Windows.Forms.TextBox
Friend WithEvents btnStart As System.Windows.Forms.Button
Private WithEvents lblResult As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtResult = New System.Windows.Forms.TextBox
Me.btnStart = New System.Windows.Forms.Button
Me.lblResult = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'txtResult
'
Me.txtResult.Location = New System.Drawing.Point(0, 16)
Me.txtResult.Name = "txtResult"
Me.txtResult.Size = New System.Drawing.Size(480, 21)
Me.txtResult.TabIndex = 0
Me.txtResult.Text = ""
'
'btnStart
'
Me.btnStart.Location = New System.Drawing.Point(384, 48)
Me.btnStart.Name = "btnStart"
Me.btnStart.TabIndex = 1
Me.btnStart.Text = "开始"
'
'lblResult
'
Me.lblResult.Location = New System.Drawing.Point(8, 48)
Me.lblResult.Name = "lblResult"
Me.lblResult.Size = New System.Drawing.Size(161, 26)
Me.lblResult.TabIndex = 2
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(488, 86)
Me.Controls.Add(Me.lblResult)
Me.Controls.Add(Me.btnStart)
Me.Controls.Add(Me.txtResult)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Me.txtResult.Text = ""
'声明一个球箱
Dim iBall() As String = {"red", "red", "red", "white", "white", "white", "white", "white", "green", "green", "green", "green", "green", "green"}
Dim i As Integer = 0
Dim result As String = ""
Dim rand As New Random
Dim temp As Integer
While (result.Split(" ").GetUpperBound(0) <> 7) '任意选取7个球
temp = CInt(rand.NextDouble() * 13)
If iBall.GetValue(temp) <> "" Then
result += iBall.GetValue(temp) & " "
iBall.SetValue("", temp) '已取出那个球置为空
End If
End While
If result.IndexOf("white") >= 0 Then '是否存在白球
While (result.Split(" ").GetUpperBound(0) <> 8) '选第八个球
temp = CInt(rand.NextDouble() * 13)
If iBall.GetValue(temp) <> "" Then
result += iBall.GetValue(temp) & " "
iBall.SetValue("", temp) '已取出那个球=""
End If
End While
Else '不存在==>将白球插入以上任意两个球之间
result = result.Insert(result.IndexOf(" ", CInt(rand.NextDouble() * (result.Length - 1))), " white")
End If
Me.txtResult.Text = result
DrawBall(result)
End Sub
Sub DrawBall(ByVal result As String)
Dim g As Graphics = Me.lblResult.CreateGraphics
Dim str() As String = result.Split(" ")
Dim i As Integer
g.Clear(Color.Blue)
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
For i = 0 To str.GetUpperBound(0)
Dim ref As New Rectangle(i * 20, 3, 20, 20)
Select Case str(i)
Case "red"
g.DrawEllipse(Pens.Black, ref)
g.FillEllipse(New SolidBrush(Color.Red), ref)
Case "green"
g.DrawEllipse(Pens.Black, ref)
g.FillEllipse(New SolidBrush(Color.Green), ref)
Case "white"
g.DrawEllipse(Pens.Black, ref)
g.FillEllipse(New SolidBrush(Color.White), ref)
Case Else
g.DrawEllipse(Pens.Black, ref)
g.FillEllipse(New SolidBrush(Color.Black), ref)
End Select
Next
End Sub
End Class