vb.net - 我在 Visual Basic 中计算时差时遇到困难
问题描述
我正在尝试构建一个以分钟为单位计算时间差的程序。我通过从完成时间中减去开始时间来做到这一点。我有四个文本框,其中两个用于小时 (h) 的时间单位,另外两个用于分钟 (m)。
a
是以小时为单位的开始时间,是以小时c
为单位的结束时间。它们乘以 60 以将小时转换为分钟。我想计算下午 5 点 40 分和晚上 7 点 15 分之间的时差,但是当答案应该是 95 时,有些结果是 567。
这不是家庭作业,我是澳大利亚的一个懒惰的学车司机,想创建一个简单的程序,以分钟为单位计算行程时间。
有人可以告诉我我做错了什么吗?
这是我的代码:
Public Class Calc
Dim Product
Dim a, b, c, d As Integer
Private Sub Enter_Click(sender As Object, e As EventArgs) Handles Enter.Click
a = Val(TextBox1.Text) * 60
b = Val(TextBox2.Text)
c = Val(TextBox3.Text) * 60
d = Val(TextBox4.Text)
Product = (c - a + d - b)
Time.Text = ("Driving Time: " & Product)
End Sub
End Class
解决方案
I wasn't going to post this but I couldn't let the use of TimeSerial
by Dy.Lee go unanswered. This code uses reasonable variable names, uses the correct type for time periods, i.e. TimeSpan
, and also compiles with Option Strict On
, which it should pretty much always be. I'd get rid of that Val
usage too but I couldn't be bothered here.
Private Sub Enter_Click(sender As Object, e As EventArgs) Handles Enter.Click
Dim startHours = CInt(Val(TextBox1.Text))
Dim startMinutes = CInt(Val(TextBox2.Text))
Dim endHours = CInt(Val(TextBox3.Text))
Dim endMinutes = CInt(Val(TextBox4.Text))
Dim startTime As New TimeSpan(startHours, startMinutes, 0)
Dim endTime As New TimeSpan(endHours, endMinutes, 0)
Dim timeDifference = endTime - startTime
Time.Text = ("Driving Time: " & timeDifference.TotalMinutes)
End Sub
EDIT: It also declares variables in the appropriate place, i.e. in the method they're being used in. If you're using those same variables elsewhere then you'd have to stick with fields but I'm guessing that you're not doing so.
EDIT: Here's a version without the dodgy Val
calls and some proper validation. You could combine all the If
statements into one but separating them allows you to display different messages based on the type of issue.
Private Sub Enter_Click(sender As Object, e As EventArgs) Handles Enter.Click
Dim startHours As Integer
Dim startMinutes As Integer
Dim endHours As Integer
Dim endMinutes As Integer
If Integer.TryParse(TextBox1.Text, startHours) AndAlso
Integer.TryParse(TextBox2.Text, startMinutes) AndAlso
Integer.TryParse(TextBox3.Text, endHours) AndAlso
Integer.TryParse(TextBox4.Text, endMinutes) Then
If startHours < 24 AndAlso
startMinutes < 60 AndAlso
endHours < 24 AndAlso
endMinutes < 60 Then
Dim startTime As New TimeSpan(startHours, startMinutes, 0)
Dim endTime As New TimeSpan(endHours, endMinutes, 0)
If startTime < endTime Then
Dim timeDifference = endTime - startTime
Time.Text = ("Driving Time: " & timeDifference.TotalMinutes)
Else
'Notify user of invalid input.
End If
Else
'Notify user of invalid input.
End If
Else
'Notify user of invalid input.
End If
End Sub
推荐阅读
- java - 通过 Charset 确定一个特殊字符
- java - Java Stream 平均值的最大值
- azure - 在 JSON DSC 中找不到 AdminCreds 参数
- windows - Sesman-Xvnc 对每个用户都抛出密码失败
- java - android.content.res.Resources$NotFoundException 加载活动时
- typescript - 为什么 cmd 中的“tsc -v”不显示打字稿版本?
- git - 澄清 GitHub 的拉取请求如何工作和一般合并。必须做什么?
- cordova - 在 Wifi 打开/关闭之前,Ionic Network 插件无法识别连接
- python - 如何在不使用 Djstatic 和 Whitenoise 的情况下添加静态文件?
- java - 如何改进隐藏/显示 NavigationView 菜单项的方式?