You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Basic如何打印数组已填充部分?骰子游戏代码求助

问题分析与解决方案

首先,你的代码里有两个核心问题:数组的存储逻辑有误,以及无法直接打印整个数组。我们一步步来解决:

1. 数组定义与存储逻辑的修正

你当前的amountrolls(100)是固定大小的数组(索引范围0-100),但外层循环i从1到100的设计有问题:因为内层的While循环会让用户多次掷骰子,而i只会递增一次,这会导致数组里的很多位置被覆盖,或者无法完整记录所有掷骰结果。

建议改成动态列表(比固定数组更灵活)来实时记录每一次掷骰子的结果:

' 替换原来的Dim amountrolls(100) As Integer
Dim amountrolls As New List(Of Integer)()

然后在每次生成roll值后,直接添加到列表里:

amountrolls.Add(roll) ' 替换原来的amountrolls(i) = roll

这样不管用户掷多少次骰子,所有结果都会被完整记录下来。

2. 如何打印所有过往骰子结果

VB.NET无法直接通过Console.WriteLine(数组名)来打印整个数组,你需要遍历元素,或者用String.Join快速拼接成可读的字符串:

方法一:用String.Join快速打印

如果用List(Of Integer),可以一行代码搞定:

Console.WriteLine("过往骰子结果:" & String.Join(", ", amountrolls))

如果还是想用固定数组,可以过滤掉未赋值的默认0值:

Console.WriteLine("过往骰子结果:" & String.Join(", ", amountrolls.Where(Function(x) x <> 0)))

方法二:循环遍历打印

如果想逐个输出并自定义格式,比如标注次数,可以用循环:

Console.WriteLine("过往骰子结果:")
For index As Integer = 0 To amountrolls.Count - 1
    Console.WriteLine($"第 {index + 1} 次:{amountrolls(index)}")
Next

3. 完整修正后的代码

这里给你整理了修复后的完整代码,优化了逻辑并添加了结果打印功能:

Module Module1
    Sub Main()
        ' 用List存储所有掷骰结果,灵活记录任意次数
        Dim amountrolls As New List(Of Integer)()
        Dim cash As Integer = 1
        Dim keepPlaying As Boolean = True

        While keepPlaying
            Console.WriteLine("Do you want to roll the dice ?")
            Dim YorN As String = Console.ReadLine().Trim().ToLower()

            If YorN = "no" Then
                Console.WriteLine($"You won £{cash}")
                ' 打印所有过往结果
                Console.WriteLine(vbCrLf & "所有过往骰子结果:")
                Console.WriteLine(String.Join(", ", amountrolls))
                Console.ReadLine()
                keepPlaying = False
            ElseIf YorN = "yes" Then
                Randomize()
                Dim roll As Integer = Int((6 * Rnd()) + 1)
                ' 记录本次掷骰结果
                amountrolls.Add(roll)

                Select Case roll
                    Case 1
                        Console.WriteLine("You rolled a 1 and lost all your money!!")
                        cash = 0
                    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 *= 2
                End Select

                Console.WriteLine($"you have £{cash}")
                Console.WriteLine("Do you still want to play?")
                YorN = Console.ReadLine().Trim().ToLower()
                If YorN = "no" Then
                    keepPlaying = False
                End If
            Else
                Console.WriteLine("Please enter 'yes' or 'no'!")
            End If
        End While

        Console.WriteLine($"Final cash: £{cash}")
        Console.ReadLine()
    End Sub
End Module

额外优化点

  • 把用户输入统一转成小写(ToLower()),避免重复判断大小写
  • 去掉了无意义的cash = cash赋值语句
  • 用While keepPlaying替代原来的外层For循环,不再限制用户只能玩100次
  • 添加了输入合法性判断,防止用户输入非yes/no的内容

内容的提问来源于stack exchange,提问作者Nathan Williams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:17:35