有没有办法保持用户输入数字的长度,以防止删除多余的0?

时间:2019-04-10 20:00:22

标签: vb.net

我正在创建糖尿病管理算法,并且试图找到一种方法来将用户输入的时间段保持在4位

我一直在Google上进行搜索,但是我只能找到如何检查变量的长度,而我已经知道该怎么做。

 Sub timeBlocks()
        Dim file As String = "C:\Users\Connor\Documents\Visual Studio 2017\Projects\meterCodeMaybe\TIMEBLOCKS.txt"
        Dim blockNum As Integer
        Console.WriteLine("Please be sure to enter times as a 24 hour value, rather than 12 hour, otherwise the input will not be handled.")
        Console.Write("Please enter the amount of time blocks you require for your day: ")
        blockNum = Console.ReadLine()
        Dim timeA(blockNum - 1) As Integer
        Dim timeB(blockNum - 1) As Integer
        Dim sensitivity(blockNum - 1) As Integer
        Dim ratio(blockNum - 1) As Integer
        For i = 0 To (blockNum - 1)
            Console.WriteLine("Please enter the start time of your time block")
            timeA(i) = Console.ReadLine()
            Console.WriteLine("Please enter the end time of your time block")
            timeB(i) = Console.ReadLine()
            Console.WriteLine("Please enter the ratio for this time block (Enter the amount of carbs that go into 1 unit of insulin)")
            ratio(i) = Console.ReadLine()
            Console.WriteLine("Please enter the insulin sensitivity for this time block 
(amount of blood glucose (mmol/L) that is reduced by 1 unit of insulin.)")
            sensitivity(i) = Console.ReadLine()
            FileOpen(1, file, OpenMode.Append)
            PrintLine(1, Convert.ToString(timeA(i)) + "-" + Convert.ToString(timeB(i)) + " 1:" + Convert.ToString(ratio(i)) + " Insulin Sensitivity:" + Convert.ToString(sensitivity(i)) + " per mmol/L")

            FileClose(1)
        Next
    End Sub

基本上,我希望用户能够为其时区输入4位数字,以匹配24小时时间,因此,如果他们输入0000,则显示为该数字,但是,它会删除所有以前的0和将其设置为0。

2 个答案:

答案 0 :(得分:1)

也许用4个前导0填充数字:

Right(String(digits, "0") & timeA(i), 4)

或者,将值存储为字符串,以便可以将其以原始形式打印出来。

答案 1 :(得分:0)

我已经编写了一个功能,可以让用户获得24小时的格式化时间,希望对您有所帮助:

  Public Function Read24HFormatTime() As String
        Dim str As String = String.Empty
        While True
            Dim c As Char = Console.ReadKey(True).KeyChar
            If c = vbCr Then Exit While
            If c = vbBack Then
                If str <> "" Then
                    str = str.Substring(0, str.Length - 1)
                    Console.Write(vbBack & " " & vbBack)
                End If
            ElseIf str.Length < 5 Then
                If Char.IsDigit(c) OrElse c = ":" Then
                    If str.Length = 0 Then
                        ' allow 0, 1 or 2 only 
                        If c = "0" OrElse c = "1" OrElse c = "2" Then
                            Console.Write(c)
                            str += c
                        End If
                    ElseIf str.Length = 1 Then
                        If str = "0" Then
                            'allow 1 to 9
                            If c <> ":" Then
                                If CInt(c.ToString) >= 1 AndAlso CInt(c.ToString) <= 9 Then
                                    Console.Write(c)
                                    str += c
                                End If
                            End If
                        ElseIf str = "1" Then
                            'allow  0 to 9
                            If c <> ":" Then
                                If CInt(c.ToString) >= 0 AndAlso CInt(c.ToString) <= 9 Then
                                    Console.Write(c)
                                    str += c
                                End If
                            End If
                        ElseIf str = "2" Then
                            'allow  0 to 4
                            If c <> ":" Then
                                If CInt(c.ToString) >= 0 AndAlso CInt(c.ToString) <= 4 Then
                                    Console.Write(c)
                                    str += c
                                End If
                            End If
                        End If
                    ElseIf str.Length = 2 Then
                        'allow  ":" only
                        If c = ":" Then
                            Console.Write(c)
                            str += c
                        End If
                    ElseIf str.Length = 3 Then
                        If str = "24:" Then
                            'allow  0 only
                            If c = "0" Then
                                Console.Write(c)
                                str += c
                            End If
                        Else
                            'allow  0 to 5
                            If c <> ":" Then
                                If CInt(c.ToString) >= 0 AndAlso CInt(c.ToString) <= 5 Then
                                    Console.Write(c)
                                    str += c
                                End If
                            End If
                        End If
                    ElseIf str.Length = 4 Then
                        If str.Substring(0, 3) = "24:" Then
                            'allow  0 only
                            If c = "0" Then
                                Console.Write(c)
                                str += c
                            End If
                        Else
                            'allow  0 to 9
                            If c <> ":" Then
                                If CInt(c.ToString) >= 0 AndAlso CInt(c.ToString) <= 9 Then
                                    Console.Write(c)
                                    str += c
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End While
        Return str
    End Function

用户只能输入23:59 08:15 13:10之类的时间,而不能输入35:10 90:00 25:13 {{ 1}}

这是示例代码,向您展示如何使用它:

10:61