Visual Basic随机数范围选择器 [英] Visual basic random number range selector

查看:81
本文介绍了Visual Basic随机数范围选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中制作一个迷你游戏,你必须解决x的值,这是随机生成的,具体取决于用户选择的难度。虽然当我尝试这样做并通过说console.writeline(x)测试它时,它说
x由于它的保护级别而无法访问。 

I am trying to make a mini game in my project were you have to solve the value of x which is randomly generated depending on the difficulty that the user chooses. Although when i try to do this and test it out by saying console.writeline(x) it says that x is inaccessible due to it's protection level. 

        If userinput = "a" Then

            Dim x As Integer = Rnd.Next(1, 10)

        ElseIf userinput = "b" Then
            Dim x As Integer = Rnd.Next(1, 20)

        ElseIf userinput = "c" Then
            Dim x As Integer = Rnd.Next(1, 30)

        End If
        Console.Clear()
        Console.WriteLine("Try and guess x.")

        userinput = Console.ReadLine
        Console.WriteLine(x)

有没有人知道如何解决这个问题?

Does anybody know how to fix this?

推荐答案

这是因为'范围'需要事先声明x。

It's because of 'scoping' where x needs to be declared before hand.

Module Module1

    Sub Main()
        Dim userinput As String = ""
        Dim x As Integer
        Dim Rnd As Random = New Random()


        If userinput = "a" Then

            x = Rnd.Next(1, 10)

        ElseIf userinput = "b" Then
            x = Rnd.Next(1, 20)

        ElseIf userinput = "c" Then
            x = Rnd.Next(1, 30)

        End If
        Console.Clear()
        Console.WriteLine("Try and guess x.")

        userinput = Console.ReadLine
        Console.WriteLine(x)

    End Sub

End Module


这篇关于Visual Basic随机数范围选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆