我正在编写一个程序来计算在一个输入框中输入英里/小时时行进的距离以及在另一个输入框中输入的小时数。当我运行程序时,它只是告诉我你输入的小时数重复的距离。我需要它按顺序显示每小时多少英里。例如,如果用户输入60英里/小时3小时,我希望它显示如下列表:
1小时60英里 2小时120英里 3小时180英里
这是我到目前为止编写的代码:
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim intSpeed As Integer
Dim intHours As Integer
Dim intDistance As Integer = 0
Dim strSpeed As String
Dim strHours As String
Dim intCount As Integer = 0
lstDistance.Items.Clear()
lstDistance.Items.Add("Hours: Distance: ")
strSpeed = InputBox("Enter speed in miles-per-hour", "Speed")
'convert to integer
If Integer.TryParse(strSpeed, intSpeed) Then
Do Until intCount = 1
strHours = InputBox("Input hours Traveled", "Hours")
If Integer.TryParse(strHours, intHours) Then
intCount += 1
End If
Loop
For intCount = 1 To intHours
intDistance = intSpeed * intHours
lstDistance.Items.Add(intHours.ToString & " " & intDistance.ToString & " Miles")
Next
End If
End Sub
我知道有一种更简单的方法可以做到这一点,但我从中学到的这本书特别要求你使用循环来解决问题。
答案 0 :(得分:0)
查看您的For
循环。
For intCount = 1 To intHours
intDistance = intSpeed * intHours
lstDistance.Items.Add(intHours.ToString & " " & intDistance.ToString & " Miles")
Next
改变的价值是什么? intCount
,对吧?您是否在输出中使用intCount
?
答案 1 :(得分:0)
我认为你的lstDistance是一个列表框 你只需将inthoures改为intcount
For intCount = 1 To intHours
intDistance = intSpeed * intHours
lstDistance.Items.Add(intHours.ToString & " " & intDistance.ToString & " Miles")
Next
到
For intCount = 1 To intHours
intDistance = intSpeed * intCount
lstDistance.Items.Add(intCount.ToString & " " & intDistance.ToString & " Miles")
Next
因为你想显示你需要使用intcount的每小时的结果
然后你会得到你想要的结果。
1 * 60 = 60
2 * 60 = 120
3 * 60 = 180
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim intSpeed As Integer
Dim intHours As Integer
Dim intDistance As Integer = 0
Dim strSpeed As String
Dim strHours As String
Dim intCount As Integer = 0
lstDistance.Items.Clear()
lstDistance.Items.Add("Hours: Distance: ")
strSpeed = InputBox("Enter speed in miles-per-hour", "Speed")
'convert to integer
If Integer.TryParse(strSpeed, intSpeed) Then
Do Until intCount = 1
strHours = InputBox("Input hours Traveled", "Hours")
If Integer.TryParse(strHours, intHours) Then
intCount += 1
End If
Loop
For intCount = 1 To intHours
intDistance = intSpeed * intCount
lstDistance.Items.Add(intCount.ToString & " " & intDistance.ToString & " Miles")
Next
End If
End Sub