我得到的输出是-
我想得到的是-
嗨,我正在尝试创建一个程序,每天获取卡路里,并在每日摘要和每周摘要中绘制图表,但带有星号,但我想不出如何使星号显示为数字的位置,如“*”而不是5。我包括了我正在获得的输出的图片以及我应该获得的以及我到目前为止的代码。任何帮助都将不胜感激。
Public Class frmA9
'Global Variables
Dim printedHeaderDaily As Boolean = False
'Global Constaints
Const FILL_SPACE As Integer = 2
Const DAY_HEADING As String = "Day"
Const TIME_HEADING As String = "Time"
Const TOTAL_HEADING As String = " "
Const TIME_LABEL_1 As String = " 5-11 AM"
Const TIME_LABEL_2 As String = " 11-5 PM"
Const TIME_LABEL_3 As String = " 5-11 PM"
Const TIME_LABEL_4 As String = " 11-5 AM"
Const TIME_RANGE_1 As Integer = 1
Const TIME_RANGE_2 As Integer = 2
Const TIME_RANGE_3 As Integer = 3
Const TIME_RANGE_4 As Integer = 4
Const TIME_LEN As Integer = 7
Const TIME_IDX_START_1 As Integer = 5
Const TIME_IDX_START_2 As Integer = 14
Const TIME_IDX_START_3 As Integer = 23
Const TIME_IDX_START_4 As Integer = 32
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAddtoSummary.Click
Dim existingIdx As Integer = -1
Dim dayformat As String = "{0,3}" & Space(FILL_SPACE) &
"{1,7:N0}" & Space(FILL_SPACE) &
"{2,7:N0}" & Space(FILL_SPACE) &
"{3,7:N0}" & Space(FILL_SPACE) &
"{4,7:N0}"
'Is header needed
If Not printedHeaderDaily Then
'Insert header
lstDailySummary.Items.Add(String.Format(dayformat,
DAY_HEADING,
TIME_LABEL_1,
TIME_LABEL_2,
TIME_LABEL_3,
TIME_LABEL_4))
'Avoid repeating header
printedHeaderDaily = True
End If
'Search for day in existing list
existingIdx = lstDailySummary.FindString(determineDay)
'Replace existing or insert new
If existingIdx > -1 Then
'Replace
lstDailySummary.Items.Item(existingIdx) = String.Format(dayformat,
determineDay,
CInt(txt5AM.Text),
CInt(txt11AM.Text),
CInt(txt5PM.Text),
CInt(txt11PM.Text))
Else
'New
lstDailySummary.Items.Add(String.Format(dayformat,
determineDay,
CInt(txt5AM.Text),
CInt(txt11AM.Text),
CInt(txt5PM.Text),
CInt(txt11PM.Text)))
End If
'Display weekly total
updateWeeklyTotatl()
End Sub
Sub updateWeeklyTotatl()
Dim totalTimeOne As Long
Dim totalTimeTwo As Long
Dim totalTimeThree As Long
Dim totalTimeFour As Long
Dim totalWeek As Long
Dim headerformat As String = "{0,-10:S}{1,6:S}"
Dim detailFormat As String = "{0,-10}{1,6:N0}"
'Remove existing data
lstWeekSummary.Items.Clear()
'Sum individual time ranges
totalTimeOne = sumCalForTime(TIME_RANGE_1)
totalTimeTwo = sumCalForTime(TIME_RANGE_2)
totalTimeThree = sumCalForTime(TIME_RANGE_3)
totalTimeFour = sumCalForTime(TIME_RANGE_4)
'Sum entire week
totalWeek = totalTimeOne + totalTimeTwo + totalTimeThree + totalTimeFour
'Insert header row
lstWeekSummary.Items.Add(String.Format(headerformat, TIME_HEADING, TOTAL_HEADING))
'Add bar for each time range
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_1, totalTimeOne))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_2, totalTimeTwo))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_3, totalTimeThree))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_4, totalTimeFour))
End Sub
Function determineDay() As String
Dim selectedDay As String
'Identify the selected Day
If rdbSun.Checked Then
selectedDay = "Sun"
ElseIf rdbMon.Checked Then
selectedDay = "Mon"
ElseIf rdbTues.Checked Then
selectedDay = "Tues"
ElseIf rdbWed.Checked Then
selectedDay = "Wed"
ElseIf rdbThurs.Checked Then
selectedDay = "Thurs"
ElseIf rdbFri.Checked Then
selectedDay = "Fri"
ElseIf rdbSat.Checked Then
selectedDay = "Sat"
End If
Return selectedDay
End Function
Function determineStartIndex(ByVal timeRange As Integer) As Integer
Dim startIndex As Integer
'Identify start index of given time range
Select Case timeRange
Case TIME_RANGE_1
startIndex = TIME_IDX_START_1
Case TIME_RANGE_2
startIndex = TIME_IDX_START_2
Case TIME_RANGE_3
startIndex = TIME_IDX_START_3
Case TIME_RANGE_4
startIndex = TIME_IDX_START_4
End Select
Return startIndex
End Function
Function sumCalForTime(ByVal timeRange As Integer) As Long
Dim CalSum As Long
Dim startIndex As Integer
'Find index of first number in given time range
startIndex = determineStartIndex(timeRange)
'Iterate through the daily summary
'Sum all water consumed in the given range
For idxRow As Integer = 1 To lstDailySummary.Items.Count - 1
CalSum += CLng(lstDailySummary.Items.Item(idxRow).ToString.Substring(startIndex, TIME_LEN))
Next
Return CalSum
End Function
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'Remove daily entries
lstDailySummary.Items.Clear()
'Remove weekly summary
lstWeekSummary.Items.Clear()
'Ensure daily header is printed after each entry
printedHeaderDaily = False
End Sub
End Class
发布于 2020-10-11 07:00:28
您可以使用String类构造函数重载,它接受两个参数:字符和重复次数:
New String("*"c, 5) ' = *****
因此,请更改以下行:
'Add bar for each time range
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_1, totalTimeOne))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_2, totalTimeTwo))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_3, totalTimeThree))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_4, totalTimeFour))
至:
'Add bar for each time range
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_1, New String("*"c, totalTimeOne)))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_2, New String("*"c,totalTimeTwo)))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_3, New String("*"c,totalTimeThree)))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_4, New String("*"c,totalTimeFour)))
发布于 2020-10-11 07:15:09
丹妮尔
将你的数字转换成一系列的星号是很容易的。
New String("*"c, totalTimeOne)
我不认为这会解决你的问题。你正在计算一天的卡路里总量,并得出一个数字(例如,早上5点到11点的卡路里总量是2600)。然后你就有了一系列卡路里的标题。你的最高限额是18310,2600是总数的14%。假设您的列表框中最多包含200个字符。您将乘以= 28的.14*200。这样你就可以打印出28个星号。
New String("*"c, 28)
这可能看起来像这样:
Sub updateWeeklyTotatl()
Dim totalTimeOne As Long
Dim totalTimeTwo As Long
Dim totalTimeThree As Long
Dim totalTimeFour As Long
Dim totalWeek As Long
Dim totalAsterisk As Long = 200 'set this to your total number of Asterisks possible
Dim maxWeekSummary As Long = 18310 'set this to the max number in chart
Dim headerformat As String = "{0,-10:S}{1,6:S}"
Dim detailFormat As String = "{0,-10}{1,6:N0}"
'Remove existing data
lstWeekSummary.Items.Clear()
'Sum individual time ranges
totalTimeOne = sumCalForTime(TIME_RANGE_1)
totalTimeTwo = sumCalForTime(TIME_RANGE_2)
totalTimeThree = sumCalForTime(TIME_RANGE_3)
totalTimeFour = sumCalForTime(TIME_RANGE_4)
'Sum entire week
totalWeek = totalTimeOne + totalTimeTwo + totalTimeThree + totalTimeFour
'Insert header row
lstWeekSummary.Items.Add(String.Format(headerformat, TIME_HEADING, TOTAL_HEADING))
'Add bar for each time range
' this formula will calculate the number of asterisks to show CInt((totalTimeOne / maxWeekSummary) * totalAsterisk)
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_1, New String("*"c, CInt((totalTimeOne / maxWeekSummary) * totalAsterisk))))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_2, New String("*"c, CInt((totalTimeTwo / maxWeekSummary) * totalAsterisk))))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_3, New String("*"c, CInt((totalTimeThree / maxWeekSummary) * totalAsterisk))))
lstWeekSummary.Items.Add(String.Format(detailFormat, TIME_LABEL_4, New String("*"c, CInt((totalTimeFour / maxWeekSummary) * totalAsterisk))))
End Sub
https://stackoverflow.com/questions/64298517
复制相似问题