用VB.NET将二进制数转换为十进制数
我是这么遍的,但是最后一个输出是0,高手看一下是怎么回事ModuleModule1SubMain()Dimb,w,i,sumAsIntegerDimaAsLongCons...
我是这么遍的,但是最后一个输出是0,高手看一下是怎么回事
Module Module1
Sub Main()
Dim b, w, i, sum As Integer
Dim a As Long
Console.WriteLine("请输入一个二进制数")
a = Console.ReadLine()
Console.WriteLine("请输入这个二进制数的位数")
w = Console.ReadLine()
b = w
sum = 0
For i = b To 0
If a - (1 * 10 ^ b) >= 0 Then
sum = sum + 1 * 10 ^ b
a = a - (1 * 10 ^ b)
b = b - 1
End If
Next
Console.WriteLine(sum)
Console.ReadLine()
End Sub
End Module 展开
Module Module1
Sub Main()
Dim b, w, i, sum As Integer
Dim a As Long
Console.WriteLine("请输入一个二进制数")
a = Console.ReadLine()
Console.WriteLine("请输入这个二进制数的位数")
w = Console.ReadLine()
b = w
sum = 0
For i = b To 0
If a - (1 * 10 ^ b) >= 0 Then
sum = sum + 1 * 10 ^ b
a = a - (1 * 10 ^ b)
b = b - 1
End If
Next
Console.WriteLine(sum)
Console.ReadLine()
End Sub
End Module 展开
展开全部
.NET 程序写成你这样也算厉害了,好好看看下面的代码
Sub Main()
Console.WriteLine("请输入一个二进制数")
Console.WriteLine(bin2int(Console.ReadLine()))
Console.ReadKey()
End Sub
Function bin2int(ByVal bin As String) As Integer
Dim i As Long
For i = 1 To bin.Length
bin2int = bin2int * 2 + Val(Mid(bin, i, 1))
Next i
End Function
Sub Main()
Console.WriteLine("请输入一个二进制数")
Console.WriteLine(bin2int(Console.ReadLine()))
Console.ReadKey()
End Sub
Function bin2int(ByVal bin As String) As Integer
Dim i As Long
For i = 1 To bin.Length
bin2int = bin2int * 2 + Val(Mid(bin, i, 1))
Next i
End Function
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Sub Main()
Console.WriteLine("请输入一个二进制数")
Console.WriteLine(bin2int(Console.ReadLine()))
Console.ReadKey()
End Sub
Function bin2int(ByVal bin As String) As Integer
Dim i As Long
For i = 1 To bin.Length
bin2int = bin2int * 2 + Val(Mid(bin, i, 1))
Next i
End Function
Console.WriteLine("请输入一个二进制数")
Console.WriteLine(bin2int(Console.ReadLine()))
Console.ReadKey()
End Sub
Function bin2int(ByVal bin As String) As Integer
Dim i As Long
For i = 1 To bin.Length
bin2int = bin2int * 2 + Val(Mid(bin, i, 1))
Next i
End Function
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询