【问题标题】:VB.net Console - Displaying a random number that was generatedVB.net 控制台 - 显示生成的随机数
【发布时间】:2016-01-03 22:22:31
【问题描述】:

我正在 VB.net 控制台中制作一个简单的基于文本的冒险,其中包括战斗。在战斗场景中,我希望游戏生成一个随机数来确定你受到的伤害。 这是我当前的代码:

Module Module1
    Dim rng As New Random
    Dim strmod As Integer = 1
    Dim yourhit As Integer

    Sub Main()
        Console.WriteLine("Welocome to the training grounds!")
        yourhit = (rng.Next(1, 4) + strmod)
        Console.WriteLine("You hit the dummy and deal {0} damage to it!",
                          yourhit)

    End Sub
End Module

现在我想知道我的随机生成的数字是否可以在不输入另一个变量(在本例中为 yourhit 变量)的情况下显示。我试过简单地用 rng 而不是 yourhit 但有一条消息说 You hit the dummy 并对其造成 System.Random 伤害!

我是 Visual Basic 的新手,因此我们将不胜感激!

【问题讨论】:

    标签: vb.net random numbers basic


    【解决方案1】:

    你应该能够做到:

    Console.WriteLine("You hit the dummy and deal {0} damage to it!",
                      (rng.Next(1, 4) + strmod))
    

    【讨论】:

    • 但是如果我想重复使用稍后将在这里滚动的数字怎么办?我有可能以某种方式做到这一点吗?编辑:我想创建一个 HP 系统,因此我希望将数字显示给用户,然后我希望从设定的数字中扣除相同的数字。
    • 变量旨在重复使用,这就是它们存在的原因。如果这是您的意图,那么您的原始代码是正确的。
    • 好的,那谢谢。我不确定我是否做得对,所以我问了以防万一。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2019-01-12
    • 2014-07-09
    • 1970-01-01
    相关资源
    最近更新 更多