Help... Again...
-
Assignment It’s often necessary to convert between units. In this exercise, you will create two functions for converting between units of distance. The first function will be called ConvertMilesToKilometers(), which will accept one parameter for the number of miles. It will return the equivalent number of kilometers. The second function will be called ConvertKilometersToMiles() and will accept kilometers as its parameter; this will return the equivalent number of miles. The main method will ask the user if he or she wants to convert miles to kilometers or kilometers to miles. It will then ask for the number of miles or kilometers. It will call the appropriate method and display the converted value. To convert miles to kilometers, divide miles by 0.62137. To convert kilometers to miles, multiply kilometers by 0.62137. Example Do you want to convert to Miles or Kilometers? (M or K): K Enter number of Kilometers: 3.5 3.5 kilometers equals 2.174795 miles.
-
Assignment It’s often necessary to convert between units. In this exercise, you will create two functions for converting between units of distance. The first function will be called ConvertMilesToKilometers(), which will accept one parameter for the number of miles. It will return the equivalent number of kilometers. The second function will be called ConvertKilometersToMiles() and will accept kilometers as its parameter; this will return the equivalent number of miles. The main method will ask the user if he or she wants to convert miles to kilometers or kilometers to miles. It will then ask for the number of miles or kilometers. It will call the appropriate method and display the converted value. To convert miles to kilometers, divide miles by 0.62137. To convert kilometers to miles, multiply kilometers by 0.62137. Example Do you want to convert to Miles or Kilometers? (M or K): K Enter number of Kilometers: 3.5 3.5 kilometers equals 2.174795 miles.
This is what I have so far, but I can't get past putting in the number of units to use. I feel like I'm never going to get a hang of this coding stuff. :( Module Module1 Dim miles As Decimal = 0 Dim kilometers As Decimal = 0 Dim answer As String Sub Main() Console.WriteLine("Do you want to convert miles or kilometers?") answer = Console.ReadLine() If answer = "miles" Then Console.WriteLine("What is the number of miles?") Console.ReadLine() Call convertMilesToKilometers() Console.WriteLine("The converted value is: " & kilometers) ElseIf answer = "kilometers" Then Console.WriteLine("What is the number of kilometers?") Console.ReadLine() Call convertKilometersToMiles() Console.WriteLine("The converted value is: " & miles) End If Console.ReadLine() End Sub Function convertMilesToKilometers() As Decimal kilometers = miles / 0.62137 Return kilometers End Function Function convertKilometersToMiles() As Decimal miles = kilometers * 0.62137 Return miles End Function End Module
-
This is what I have so far, but I can't get past putting in the number of units to use. I feel like I'm never going to get a hang of this coding stuff. :( Module Module1 Dim miles As Decimal = 0 Dim kilometers As Decimal = 0 Dim answer As String Sub Main() Console.WriteLine("Do you want to convert miles or kilometers?") answer = Console.ReadLine() If answer = "miles" Then Console.WriteLine("What is the number of miles?") Console.ReadLine() Call convertMilesToKilometers() Console.WriteLine("The converted value is: " & kilometers) ElseIf answer = "kilometers" Then Console.WriteLine("What is the number of kilometers?") Console.ReadLine() Call convertKilometersToMiles() Console.WriteLine("The converted value is: " & miles) End If Console.ReadLine() End Sub Function convertMilesToKilometers() As Decimal kilometers = miles / 0.62137 Return kilometers End Function Function convertKilometersToMiles() As Decimal miles = kilometers * 0.62137 Return miles End Function End Module
What does "I can't get past putting in the number of units to use" mean? You are ignoring any user input except for the,
answer = Console.ReadLine()
line. You would have to do something just like that to get the number of miles or kilometers that the user wants to convert....
-
What does "I can't get past putting in the number of units to use" mean? You are ignoring any user input except for the,
answer = Console.ReadLine()
line. You would have to do something just like that to get the number of miles or kilometers that the user wants to convert....
Oh, my goodness! I fixed the problem, and now everything works! Thank you!
-
Oh, my goodness! I fixed the problem, and now everything works! Thank you!
-
This is what I have so far, but I can't get past putting in the number of units to use. I feel like I'm never going to get a hang of this coding stuff. :( Module Module1 Dim miles As Decimal = 0 Dim kilometers As Decimal = 0 Dim answer As String Sub Main() Console.WriteLine("Do you want to convert miles or kilometers?") answer = Console.ReadLine() If answer = "miles" Then Console.WriteLine("What is the number of miles?") Console.ReadLine() Call convertMilesToKilometers() Console.WriteLine("The converted value is: " & kilometers) ElseIf answer = "kilometers" Then Console.WriteLine("What is the number of kilometers?") Console.ReadLine() Call convertKilometersToMiles() Console.WriteLine("The converted value is: " & miles) End If Console.ReadLine() End Sub Function convertMilesToKilometers() As Decimal kilometers = miles / 0.62137 Return kilometers End Function Function convertKilometersToMiles() As Decimal miles = kilometers * 0.62137 Return miles End Function End Module
Not entirely sure what Language you are using here but it looks very much like VB. Therefore assuming that it is VB. On the surface your code looks correct. If I had to rewrite the code it would look something like this:
Module Module1
Dim distance As Decimal = 0
Dim answer As String = nothingSub Main()
Console.WriteLine("Do you want to convert miles or kilometers? (M or K)")
answer = Console.ReadLine()Console.WriteLine("What is the distance?")
distance = Console.ReadLine()If answer = "M" Then
Console.WriteLine("The converted value is: " convertToKM(distance))
Else
Console.WriteLine("The converted value is: " & converToML(distance))
End IfConsole.ReadLine()
End SubFunction convertToKM(distance) As Decimal
Dim km as Decimal = 0km = distance / 0.62137
Return km
End FunctionFunction convertToML(distance) As Decimal
Dim ml as Decimal = 0ml = distance * 0.62137
Return ml
End FunctionEnd Module