Arrays 如何将整个阵列打印到我已达到的点(Visual Basic)?
我正在做一个骰子游戏,我想用一个数组打印所有以前的骰子输出,但我不确定我将如何打印数组,记住我不确定我是否正确地完成了数组。我已将所有代码粘贴到下面Arrays 如何将整个阵列打印到我已达到的点(Visual Basic)?,arrays,vb.net,Arrays,Vb.net,我正在做一个骰子游戏,我想用一个数组打印所有以前的骰子输出,但我不确定我将如何打印数组,记住我不确定我是否正确地完成了数组。我已将所有代码粘贴到下面 Module Module1 Sub Main() Dim amountrolls(100) As Integer Dim cash As Integer = 1 For i As Integer = 1 To 100 Console.WriteLine(
Module Module1
Sub Main()
Dim amountrolls(100) As Integer
Dim cash As Integer = 1
For i As Integer = 1 To 100
Console.WriteLine("Do you want to roll the dice ?")
Dim YorN As String = Console.ReadLine
If (YorN = "no") Or (YorN = "No") Then
Console.WriteLine("You won £" & cash)
Console.ReadLine()
Else
While (YorN = "yes") Or (YorN = "Yes")
Randomize()
Dim roll As Integer = (Int((6 * Rnd()) + 1))
Select Case roll
Case 1
amountrolls(i) = roll
Console.WriteLine("You rolled a 1 and lost all your money!!")
cash = 0
Console.WriteLine("you have £" & cash)
Console.WriteLine("Do you still want to play?")
YorN = Console.ReadLine
Case 2, 3, 4
amountrolls(i) = roll
Console.WriteLine("You rolled a " & roll & " so nothing happened.")
cash = cash
Console.WriteLine("you have £" & cash)
Console.WriteLine("Do you still want to play?")
YorN = Console.ReadLine
Case 5, 6
amountrolls(i) = roll
Console.WriteLine("You rolled a " & roll & " and doubled your money!!")
cash = cash * 2
Console.WriteLine("you have £" & cash)
Console.WriteLine("Do you still want to play?")
YorN = Console.ReadLine
End Select
End While
End If
Console.WriteLine(amountrolls())
Next
Console.WriteLine("You have £" & cash)
Console.ReadLine()
End Sub
End Module
使用.net
Random
类的一个实例。正如评论中所建议的,我们使用列表(共T个)
而不是数组。当我们问用户“是”或“否”时,给他们一个提示,告诉他们您希望他们输入什么。我调用了.ToLower
,所以我只需要检查小写字母。在这里,您可以做更多的工作来检查用户输入,但这就足够了
我们使用随机
类的.Next
方法。这将返回一个大于等于1且小于7的数字。只需。将编号添加到列表中
Private Rnd As New Random
Sub Main()
Dim amountrolls As New List(Of Integer)
Dim cash As Integer = 1
Console.WriteLine("Do you want to roll the dice? (y or n ")
Dim YorN As String = Console.ReadLine.ToLower
If (YorN = "n") Then
End
Else
While (YorN = "y")
Dim roll As Integer = Rnd.Next(1, 7)
amountrolls.Add(roll)
Select Case roll
Case 1
Console.WriteLine("You rolled a 1 and lost all your money!!")
cash = 0
Exit While
Case 2, 3, 4
Console.WriteLine("You rolled a " & roll & " so nothing happened.")
Case 5, 6
Console.WriteLine("You rolled a " & roll & " and doubled your money!!")
cash = cash * 2
End Select
Console.WriteLine("you have £" & cash)
Console.WriteLine("Do you still want to play? (y or n)")
YorN = Console.ReadLine.ToLower
End While
End If
Console.WriteLine("These are all your rolls.")
For Each i In amountrolls
Console.WriteLine(i)
Next
Console.WriteLine("You have £" & cash)
Console.ReadLine()
End Sub
如果用户掷1,则完成。根据你的规则,他永远不会得到超过零的分数,所以我们退出While循环
您在选择案例
的每个案例
中重复了相同的代码,因此我将重复代码移到了选择之后
当用户输入n时,循环不会继续,最终结果显示为列表中每个条目的
Private Rnd As New Random
Sub Main()
Dim amountrolls As New List(Of Integer)
Dim cash As Integer = 1
Console.WriteLine("Do you want to roll the dice? (y or n ")
Dim YorN As String = Console.ReadLine.ToLower
If (YorN = "n") Then
End
Else
While (YorN = "y")
Dim roll As Integer = Rnd.Next(1, 7)
amountrolls.Add(roll)
Select Case roll
Case 1
Console.WriteLine("You rolled a 1 and lost all your money!!")
cash = 0
Exit While
Case 2, 3, 4
Console.WriteLine("You rolled a " & roll & " so nothing happened.")
Case 5, 6
Console.WriteLine("You rolled a " & roll & " and doubled your money!!")
cash = cash * 2
End Select
Console.WriteLine("you have £" & cash)
Console.WriteLine("Do you still want to play? (y or n)")
YorN = Console.ReadLine.ToLower
End While
End If
Console.WriteLine("These are all your rolls.")
For Each i In amountrolls
Console.WriteLine(i)
Next
Console.WriteLine("You have £" & cash)
Console.ReadLine()
End Sub
Console.WriteLine(amountrolls(i))
或者在循环之后:amountrolls.ToList().ForEach(函数(x)Console.WriteLine(x))
这是作业吗?如果不是-或者是,但你不这样做-不要使用数组,也不要那样生成随机数。使用一个列表(整数)
,该列表将开始为空,并在向其中添加项目时实际增长。这样,您每次都可以得到整个列表,而不用担心设置了哪些数组元素,哪些没有设置。对于随机数,使用random
类。如果要以这种方式生成随机数,至少要正确地进行,即调用Randomize
一次且仅一次。此外,数组是基于零的,所以要以这种方式处理它们。忽略零索引是很糟糕的。创建数组时,可以指定上限,而不是长度。如果需要包含100个元素的数组,则指定99作为上限。然后将其从0索引到99。