VB中的数组问题

时间:2015-11-24 23:39:46

标签: arrays vb.net syntax

VB中的数组有问题。我将程序从C转换为VB,并且除了涉及在数组中输入数字的问题外,大部分都将其删除。代码如下。

    Sub convertNumber()

    Do While (numberToConvert <> 0)

        convertedNumber(digit) = numberToConvert Mod base 'Line 47
        digit += 1
        numberToConvert /= base

    Loop

End Sub

这是崩溃的特殊子。问题似乎是&#34; convertedNumber(数字)&#34;但是我无法弄明白。我尝试做一些调试,每一个似乎都工作正常。它只是我需要输入数组的值似乎打破了整个事情。整个计划发布在下面。

    Module Module1

Dim convertedNumber() As Integer
Dim numberToConvert As Integer
Dim base As Integer
Dim digit As Integer
Dim moddedNumber As Integer

Sub Main()

    'Calls in other SubModules
    Call getNumberAndBase()
    Call convertNumber()
    Call displayConvertedNumber()

    Console.ReadLine() ' Waits to terminate program

End Sub

Sub displayConvertedNumber()

    Dim baseDigit() As Char = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}

    Dim nextDigit As Integer
    Console.WriteLine("Converted Number = ")

    digit = digit - 1
    For digit As Integer = digit To 0
        nextDigit = convertedNumber(digit)
        Console.WriteLine(baseDigit(nextDigit))
        digit -= 1
    Next

    Console.WriteLine()

End Sub

Sub convertNumber()

    Do While (numberToConvert <> 0)

        convertedNumber(digit) = numberToConvert Mod base 'Line 47
        digit += 1
        numberToConvert /= base

    Loop

End Sub

Sub getNumberAndBase()

    Console.WriteLine("Number to be converted?")
    numberToConvert = Console.ReadLine()
    Console.WriteLine("Base?")
    base = Console.ReadLine()

    If base < 2 Or base > 16 Then
        Console.WriteLine("Bad base - Must be between 2 and 16")
        base = 10
    End If

End Sub

    End Module

1 个答案:

答案 0 :(得分:0)

由LarsTech发布,这是我的For循环的一个问题。通过添加&#34;步骤-1&#34;到最后,它解决了这个问题。

谢谢!