VISUAL BASIC I\'m trying to figure out how I can do the following with the progr
ID: 644145 • Letter: V
Question
VISUAL BASIC
I'm trying to figure out how I can do the following with the program I have.
Keep track how many times one of the 11 possible sums are rolled, and when rolls are complete, display the grand total for each different roll in 11 labels
CODE IS BELOW:
-----------------------------------------------------
Dim shooter As New Random()
Dim die1 As Integer
Dim die2 As Integer
Dim die1Value As Integer
Dim die2Value As Integer
Dim total As Integer
Do Until lstRolls.Items.Count = 100 ' Run Loop until 100 rolls show
die1 = shooter.Next(6)
die2 = shooter.Next(6)
die1Value = die1 + 1
die2Value = die2 + 1
total = die1Value + die2Value
lblDie1.ImageIndex = die1
lblDie2.ImageIndex = die2
lstRolls.Items.Add(die1Value.ToString() & " + " & die2Value.ToString() & _
" = " & total.ToString())
lstRolls.SelectedIndex = lstRolls.Items.Count - 1
Loop
Explanation / Answer
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim shooter As New Random()
Dim die1 As Integer
Dim die2 As Integer
Dim die1Value As Integer
Dim die2Value As Integer
Dim total As Integer
Dim icount As Integer = 0
Dim iArray(11, 1) As Integer
Dim iIndex As Integer = 0
Dim iCnt As Integer = 1
Dim iCount1 As Integer
Dim blnExists As Boolean = False
Do Until lstRolls.Items.Count = 100 ' Run Loop until 100 rolls show
die1 = shooter.Next(6)
die2 = shooter.Next(6)
die1Value = die1 + 1
die2Value = die2 + 1
total = die1Value + die2Value
' add the total to a two dimensional array along with the number of times it appears
' if already added increase the count
For iCount1 = 0 To 10
If (iArray(iCount1, 0) = total) Then
iArray(iCount1, 1) = iArray(iCount1, 1) + 1
blnExists = True
Exit For
End If
Next
' if not added add to the array
If (blnExists = False) Then
iArray(iIndex, 0) = total
iArray(iIndex, 1) = iCnt
iIndex = iIndex + 1
End If
blnExists = False
lblDie1.ImageIndex = die1
lblDie2.ImageIndex = die2
lstRolls.Items.Add(die1Value.ToString() & " + " & die2Value.ToString() & " = " & total.ToString())
lstRolls.SelectedIndex = lstRolls.Items.Count - 1
Loop
' print the values of the array to the labels
Label1.Text = iArray(0, 0) & " = " & iArray(0, 1)
Label2.Text = iArray(1, 0) & " = " & iArray(1, 1)
Label3.Text = iArray(2, 0) & " = " & iArray(2, 1)
Label4.Text = iArray(3, 0) & " = " & iArray(3, 1)
Label5.Text = iArray(4, 0) & " = " & iArray(4, 1)
Label6.Text = iArray(5, 0) & " = " & iArray(5, 1)
Label7.Text = iArray(6, 0) & " = " & iArray(6, 1)
Label8.Text = iArray(7, 0) & " = " & iArray(7, 1)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.