首页 > 解决方案 > 我在 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

标签: vb.nettime

解决方案


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

推荐阅读