vb.net - 有没有办法保持用户输入数字的长度,以防止删除额外的 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。
解决方案
也许用 4 个前导 0 填充数字:
Right(String(digits, "0") & timeA(i), 4)
或者作为替代方案,将值存储为字符串,以便可以以其原始形式打印出来。
推荐阅读
- c# - 如何在 Python 中定义作为类实例的类字段?
- java - 为什么这段代码在多次重新计算深度时检查二叉树是否平衡需要时间 O(n log n)?
- python - 使用 Python (Linux/Windows) 的网络带宽 (Mbps) 和磁盘 I/O (MB/s)
- r - 如何根据数据框中相邻单元格的值汇总数据
- python - tf.reduce_mean 的数值稳定性
- javascript - 设置格式化值中断函数
- c# - 除了从用户输入派生的 ITurnContext RecognizeAsync 之外,为 LuisRecognizer 提供预定文本上下文的最佳方法是什么?
- c++ - 为什么glm不翻译顶点
- go - 在 Go 切片中追加时超出范围
- kotlin - 错误:Kotlin:浮点文字不符合预期的浮点类型