C# 我需要为C语言编程项目的messagebox/textbox添加两个变量#
嘿,大家好,当我试图在C# 我需要为C语言编程项目的messagebox/textbox添加两个变量#,c#,C#,嘿,大家好,当我试图在文本框中显示时,我的代码很难处理。。测试进行得很顺利,直到我尝试了一些开始时间大于结束时间的东西,当我这样做时,它在我的程序-04-30上显示类似的内容,但在示例程序上显示-0430 如何使其正确显示 我的最后三行代码被读取。(这是针对我的大学课程的一个编程项目。我有我开发的表单程序,然后是一个示例程序。对于该程序,它要求给出一个开始时间和结束时间。该程序计算由于该地区的施工而产生的新到达时间(这使最初的行程延长了25%) //在文本框中显示答案,供用户查看和理解 text
文本框中显示时,我的代码很难处理。。测试进行得很顺利,直到我尝试了一些开始时间大于结束时间的东西,当我这样做时,它在我的程序-04-30上显示类似的内容,但在示例程序上显示-0430
如何使其正确显示
我的最后三行代码被读取。(这是针对我的大学课程的一个编程项目。我有我开发的表单程序,然后是一个示例程序。对于该程序,它要求给出一个开始时间和结束时间。该程序计算由于该地区的施工而产生的新到达时间(这使最初的行程延长了25%)
//在文本框中显示答案,供用户查看和理解
textBoxNewArrival.Text = string.Format("{0:d2}" + "{1:d2}", endTimeHours, endTimeMins);
整个代码是;
`//声明常量
常数int百=100,除数u六十=60;
常数双结构=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));
`
要快速解决此问题,请尝试在文本框分配中执行此操作:
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));
在这种情况下,.Remove似乎不是一个命令。我键入它是因为我只是在visual studio中的.Remove..im下面抛出了一条红色的错误行,如果这有什么区别的话?好的,很抱歉,您首先需要添加.ToString()方法:P/////////////textBoxNewArrival.Text=string.Format({0:d2}+“{1:d2]”,endTimeHours,endTimeMins.ToString().Remove(0,1));对!我一直忘了您将变量打印为int:Sphere是代码不确定发生了什么(我想我可能声明了错误的数据类型)。。(用户也可以24小时格式输入时间。在整个程序中,我已转换为分钟和返回时间。看,您可以在我给您的内容中添加另一段代码。但我不知道这样对您来说是否太“混乱”:P无论如何,请检查我的答案↑↑ 我只是用一种应该可以工作的方式编辑了它:p这样做之后,它告诉我我有构建错误..你想让我发布整个代码吗?只需复制并粘贴那一行…注意结尾有两个右括号;一个用于Math.Abs(),另一个用于String.Format()。将整个代码添加到原始帖子中。我想我在整个程序中错误地声明了数据类型。。最后一行代码是textBoxNewArrival.Text=string.Format(“{0:d2}”+“{1:d2]”,endTimeHours,endTimeMins.ToString()。Remove(0,1));这是Rick的解决方案,使用字符串操作。我的不同,使用Math.Abs()…仔细看。是的,我试过使用这个和他的,我不断地得到构建错误不确定我错了什么不确定这是否是一个打字错误,但我看到错误的格式:“{0:d2}”+“{1:d2]”,而不是:“{0:d2}”+“{1:d2}”?如果可能的话,试着用你计算的时间跨度和分钟数,并可以用合适的时间跨度正确地显示出来。不确定这意味着什么。哈哈,我对编程是全新的。我在大学里学的是编程基础,这是我上过的第一堂课。。
textBoxNewArrival.Text = string.Format("{0:d2}" + "{1:d2}", endTimeHours, Math.Abs(endTimeMins));