I don't know what equation vb will recognise for my code. to work. The user chooses weather to enter a decimal number or binary. It will then use an equation to convert that number. I have started the decimal but then got stuck, with what equation to use.
This is the code:
Module Module1
Sub Main()
Dim userchoice As String
Dim numberchoice, numchoice, newnumber As Integer
Console.WriteLine("Please choose weather to convert 1) decimal to Binary or 2)Binary to decimal.")
userchoice = Console.ReadLine()
If userchoice = 1 Then
Console.WriteLine("You are converting from a Decimal - Binary")
Console.WriteLine("Please enter the number,")
numberchoice = Console.ReadLine()
End If
If numberchoice > 128 Then
Console.WriteLine("1")
newnumber = numberchoice - 128
End If
If newnumber > 64 Then
Console.WriteLine("1")
newnumber = newnumber - 64
End If
If numberchoice > 32 Then
Console.WriteLine("1")
newnumber = newnumber - 32
End If
If numberchoice > 16 Then
Console.WriteLine("1")
newnumber = newnumber - 16
End If
If numberchoice > 8 Then
Console.WriteLine("1")
newnumber = newnumber - 8
End If
If numberchoice > 4 Then
Console.WriteLine("1")
newnumber = newnumber - 4
End If
If numberchoice > 2 Then
Console.WriteLine("1")
newnumber = newnumber - 2
End If
If numberchoice > 1 Then
Console.WriteLine("1")
newnumber = newnumber - 1
End If
Console.ReadLine()
If userchoice = 2 Then
Console.WriteLine("You're converting from Binary - Decimal")
Console.WriteLine("Please enter the binary number,")
numchoice = Console.ReadLine()
End If
End Sub
End Module
答案 0 :(得分:0)
这就是你所寻求的:
Module Module1
Sub Main()
While True
Console.WriteLine("Please choose whether to convert:")
Console.WriteLine("1.) Decimal to Binary")
Console.WriteLine("2.) Binary to Decimal")
Dim userChoice As String = Console.ReadLine()
If userChoice = 1 Then
Console.WriteLine("Converting from Decimal to Binary.")
Console.Write("Please enter a Decimal number: ")
Dim decimalNumber = Console.ReadLine()
Console.WriteLine(String.Format("Binary representation of {0} in Decimal is {1}.", decimalNumber, ToBinary(decimalNumber)))
ElseIf userChoice = 2 Then
Console.WriteLine("Converting from Binary to Decimal.")
Console.Write("Please enter a Binary number: ")
Dim binaryNumber = Console.ReadLine()
Console.WriteLine(String.Format("Decimal representation of {0} in Binary is {1}.", binaryNumber, ToDecimal(binaryNumber)))
Else
Console.WriteLine("Please enter a correct value.")
End If
Console.WriteLine(Environment.NewLine + "-------------------------------" + Environment.NewLine)
End While
End Sub
Function ToBinary(input As String) As String
Return Convert.ToString(Int64.Parse(input), 2)
End Function
Function ToDecimal(input As String) As String
Return Convert.ToString(Int64.Parse(input), 10)
End Function
End Module