首页 > 解决方案 > 有没有办法保持用户输入数字的长度,以防止删除额外的 0?

问题描述

我正在创建一个糖尿病管理算法,我正在尝试找到一种方法让用户输入的时间块保持在 4 位数

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

 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。

标签: vb.net

解决方案


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

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

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


推荐阅读