希望修复此代码...请帮助 [英] Looking to fix this code...please help

查看:68
本文介绍了希望修复此代码...请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法弄清楚为什么在生成此代码时只会将整数1从二进制转换为十进制,当您在二进制序列中添加任意零时,它会循环返回,要求您输入二进制数。



我尝试过:



模块模块1



Sub Main()



Dim lv_binary As String

Dim lv_decimal As Decimal

lv_binary = getBinary()

Console.WriteLine(lv_binary)

lv_decimal = convert2Binary(lv_binary)

Console.WriteLine(二进制:{0} ,lv_binary)

Console.WriteLine(十进制:{0},lv_decimal)

Main()

Console.ReadKey()< br $>




End Sub



函数convert2Binary(pv_binary As String)As Decimal

Dim lv_decimalNumber为十进制

Dim lv_digit为整数

Dim lv_maxElementNumber为整数

Dim lv_positionValue(pv_binary.Length - 1)As Decimal

lv_decimalNumber = 0

lv_maxElementNumber = pv_binary.Length - 1

对于lv_index = 0到lv_maxElementNumber

lv_digit = CInt(pv_binary(lv_index).ToString)

lv_positionValue(lv_index)= lv_digit * 2 ^ (lv_maxElementNumber - lv_index)

lv_decimalNumber + = lv_positionValue(lv_index)

Console.WriteLine(CStr(lv_digit)& * 2 ^&

CStr(lv_maxElementNumber - lv_index)& =&

CStr(lv_positionValue(lv_index)))



下一页



Console.WriteLine(答案是通过添加以下值组成的:)

对于lv_index = lv_positionValue.Length - 1到0步-1

如果lv_index< lv_positionValue.Length - 1然后

Console.Write(+)

否则

Console.Write()



结束如果

Console.WriteLine(CStr(lv_positionValue(lv_index))。PadLeft(5 + CStr(lv_positionValue(0))。Length))

下一页

Console.WriteLine(StrDup(CStr(lv_positionValue(0))。长度+ 5 + 1, - ))



返回lv_decimalNumber









结束功能





函数getBinary()字符串

Dim lv_binary字符串

Dim lv_valid As Boolean

Do

Console.Write(输入要转换的二进制数:)

lv_binary = Console.ReadLine

如果是IsNumeric(lv_binary)那么

lv_valid = True

对于lv_inde x = 1或0到lv_binary.Length - 1

如果lv_binary(lv_index)<> 1然后

lv_valid = False

结束如果



下一页

否则

lv_valid = False



结束如果

循环直到lv_valid

返回lv_binary





结束功能





结束模块

cant figure out why when generated this code will only convert the integer 1 from binary to decimal, when you add any zeros into your binary sequence it loops back asking you to enter a binary number.

What I have tried:

Module Module1

Sub Main()

Dim lv_binary As String
Dim lv_decimal As Decimal
lv_binary = getBinary()
Console.WriteLine(lv_binary)
lv_decimal = convert2Binary(lv_binary)
Console.WriteLine("Binary:{0}", lv_binary)
Console.WriteLine("Decimal:{0}", lv_decimal)
Main()
Console.ReadKey()


End Sub

Function convert2Binary(pv_binary As String) As Decimal
Dim lv_decimalNumber As Decimal
Dim lv_digit As Integer
Dim lv_maxElementNumber As Integer
Dim lv_positionValue(pv_binary.Length - 1) As Decimal
lv_decimalNumber = 0
lv_maxElementNumber = pv_binary.Length - 1
For lv_index = 0 To lv_maxElementNumber
lv_digit = CInt(pv_binary(lv_index).ToString)
lv_positionValue(lv_index) = lv_digit * 2 ^ (lv_maxElementNumber - lv_index)
lv_decimalNumber += lv_positionValue(lv_index)
Console.WriteLine(CStr(lv_digit) & " * 2^" &
CStr(lv_maxElementNumber - lv_index) & " = " &
CStr(lv_positionValue(lv_index)))

Next

Console.WriteLine("The answer is compose by adding the following values:")
For lv_index = lv_positionValue.Length - 1 To 0 Step -1
If lv_index < lv_positionValue.Length - 1 Then
Console.Write("+")
Else
Console.Write(" ")

End If
Console.WriteLine(CStr(lv_positionValue(lv_index)).PadLeft(5 + CStr(lv_positionValue(0)).Length))
Next
Console.WriteLine(StrDup(CStr(lv_positionValue(0)).Length + 5 + 1, "-"))

Return lv_decimalNumber




End Function


Function getBinary() As String
Dim lv_binary As String
Dim lv_valid As Boolean
Do
Console.Write("Enter a binary number to convert:")
lv_binary = Console.ReadLine
If IsNumeric(lv_binary) Then
lv_valid = True
For lv_index = 1 Or 0 To lv_binary.Length - 1
If lv_binary(lv_index) <> "1" Then
lv_valid = False
End If

Next
Else
lv_valid = False

End If
Loop Until lv_valid
Return lv_binary


End Function


End Module

推荐答案

引用:

当你在二进制文件中添加任何零顺序它循环回来要求你输入二进制数

when you add any zeros into your binary sequence it loops back asking you to enter a binary number



可能是因为这正是你告诉它要做的:


Probably because that's exactly what you've told it to do:

If lv_binary(lv_index) <> "1" Then 
    lv_valid = False



更改您的代码,使其也接受 0 作为有效字符:


Change your code so that it also accepts 0 as a valid character:

For lv_index = 0 To lv_binary.Length - 1
    If lv_binary(lv_index) <> "1"c AndAlso lv_binary(lv_index) <> "0"c Then
        lv_valid = False
    End If
Next


当你不明白你的代码是什么正在做或为什么它做它做的,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。

Debugger - 维基百科,免费的百科全书 [ ^ ]

Visual Basic / Visual Studio视频教程 - 基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.
Debugger - Wikipedia, the free encyclopedia[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于希望修复此代码...请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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