[英]i need to add two variables together for a messagebox/textbox for a programming project IN C#
嘿,所以嘗試在textbox
顯示代碼時,我遇到了textbox
。.測試一直很好,直到我嘗試按開始時間大於結束時間的次數顯示某些內容時,在我的程序-04-30上是這樣的,但是在示例程序上它顯示為-0430。
我如何使其正確顯示?
我的最后三行代碼已讀。 (這是針對我大學課程的一個編程項目。我已經開發了我的表單程序,然后是一個示例。對於該程序,它要求給出開始時間和結束時間。該程序計算出新的到達時間到該地區的建築(這會使原始行程延長25%)
int endTimeHours = (int) totalTimeMins / DIVISOR_SIXTY;
int endTimeMins = (int)totalTimeMins - (endTimeHours * DIVISOR_SIXTY);
//在文本框中顯示答案,供用戶查看和理解。
textBoxNewArrival.Text = string.Format("{0:d2}" + "{1:d2}", endTimeHours, endTimeMins);
整個代碼是; `//聲明常量const int HUNDRED = 100,DIVISOR_SIXTY = 60; const double CONSTRUCTION = 1.25;
//get start time from user
int startTime = int.Parse(textBoxStart.Text);
//get end time from user
int endTime = int.Parse(textBoxEnd.Text);
//separate and convert to hours and minutes for start time
int hoursStart = startTime / HUNDRED;
int minsStart = startTime % HUNDRED;
int totalMinutesStart = (hoursStart * DIVISOR_SIXTY) + minsStart;
//separate and convert to hours and minutes for end time
int hoursEnd = endTime / HUNDRED;
int minEnd = endTime % HUNDRED;
int totalMinutesEnd = (hoursEnd * DIVISOR_SIXTY) + minEnd;
//find total duration by subtracting total end times in minutes from total start time in minutes
int totalDuration = totalMinutesEnd - totalMinutesStart;
//take the totalDuration or time between start and end and multiply by 1.25 for construction time.
double construction = totalDuration * CONSTRUCTION;
//take totalmintuesstart from begining and add construction.
double totalTimeMins = totalMinutesStart + construction;
//find and convert the answers into hours and minutes total
int endTimeHours = (int) totalTimeMins / DIVISOR_SIXTY;
int endTimeMins = (int) totalTimeMins - (endTimeHours * DIVISOR_SIXTY);
//display answer in textbox for user to see and understand.
textBoxNewArrival.Text = string.Format("{0:d2}" + "{1:d2]", endTimeHours, endTimeMins.ToString().Remove(0, 1));
`
為了快速解決方案,您應該嘗試在textBox分配中執行以下操作:
textBoxNewArrival.Text = string.Format("{0:d2}" + "{1:d2}", int.Parse(endTimeHours, endTimeMins.ToString().Remove(0,1)));
這是我所做的測試,目的是檢查這次是否有效:
class Forms
{
public static void Main(string[] args)
{
int endTimeHours = -4;;
int endTimeMins = -30;
string temp = string.Format("{0:d2}" + "{1:d2}", endTimeHours, int.Parse(endTimeMins.ToString().Remove(0, 1)));
Console.WriteLine(temp);
}
}
只需保留第一個值的符號,並始終使用第二個值的絕對值?
textBoxNewArrival.Text = string.Format("{0:d2}" + "{1:d2}", endTimeHours, Math.Abs(endTimeMins));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.