我们最近在课堂上报道了“如果陈述等......”,我遇到了这个问题。 (为我可怜的英语道歉)
这就是问题:
这是我的错误答案:
private void button1_Click(object sender, EventArgs e)
{
//Declaring Variables
int totalSeconds;
int hours;
int minutes;
int minutesRemainder;
int hoursRemainderMinutes;
int hoursRemainderSeconds;
// Parsing and calculations
totalSeconds = int.Parse(textBox1.Text);
minutes = totalSeconds / 60;
minutesRemainder = totalSeconds % 60;
hours = minutes / 60;
hoursRemainderMinutes = minutes % 60;
hoursRemainderSeconds = hoursRemainderMinutes % 60;
if (totalSeconds >= 60)
{
MessageBox.Show(totalSeconds.ToString());
}
else if (totalSeconds >= 3600)
{
MessageBox.Show(minutes.ToString() + " minutes, " + minutesRemainder.ToString() + " seconds");
}
else if (totalSeconds >= 84600)
{
MessageBox.Show(hours.ToString() + " hours, " + hoursRemainderMinutes.ToString() + " minutes, " + hoursRemainderSeconds.ToString() + " seconds");
}
}
}
}
运行时,我的程序不会计算任何内容。我究竟做错了什么?
答案 0 :(得分:4)
您应该使用TimeSpan.FromSeconds method。
它将为您提供TimeSpan
结构实例,您可以访问:
TotalDays
TotalHours
TotalMinutes
性质。
修改强>
他们在评论中说,你想要在不使用任何库的情况下实现这一目标。 那么方法(就你的目标而言):
int totalSeconds = ....;///
int totalMinutes = totalSeconds / 60;
int totalHours = totalMinutes / 60;
int totalDays = totalHours / 24;
if (totalDays > 0){
//show days
} else if (totalHours > 0){
//show hours
} else if (totalMinutes > 0){
//show minutes
} else {
//show seconds
}
答案 1 :(得分:3)
确定。假设您不想使用TimeSpan。你的代码非常接近工作。你的问题是你的最后一个“else if”语句应该用if语句反转:
if (totalSeconds >= 86400)
{
Console.WriteLine(days.ToString() " days," + hours.ToString() + " hours, " + hoursRemainderMinutes.ToString() + " minutes, " + hoursRemainderSeconds.ToString() + " seconds");
}
else if (totalSeconds >= 3600)
{
Console.WriteLine(hours.ToString() + " hours, " + hoursRemainderMinutes.ToString() + " minutes, " + hoursRemainderSeconds.ToString() + " seconds");
}
else if (totalSeconds >= 60)
{
Console.WriteLine(minutes.ToString() + " minutes, " + minutesRemainder.ToString() + " seconds");
}
else
{
Console.WriteLine(totalSeconds.ToString());
}
这样就可以了。
答案 2 :(得分:0)
以下是如何计算所有值并确定输出中需要哪些值
int seconds = totalSeconds % 60;
int totalMinutes = totalSeconds / 60;
int minutes = totalMinutes % 60;
int totalHours = totalMinutes / 60;
int hours = totalHours % 24;
int totalDays = totalHours / 24;
if (totalDays > 0)
{
Console.Write(totalDays + " Days ");
}
if (totalHours > 0)
{
Console.Write(hours + " Hours ");
}
if (totalMinutes > 0)
{
Console.Write(minutes + " Minutes ");
}
Console.WriteLine(seconds + " Seconds");
或使用StringBuilder
,以便您可以在MessageBox
int seconds = totalSeconds % 60;
int totalMinutes = totalSeconds / 60;
int minutes = totalMinutes % 60;
int totalHours = totalMinutes / 60;
int hours = totalHours % 24;
int totalDays = totalHours / 24;
StringBuilder builder = new StringBuilder;
if (totalDays > 0)
{
builder.Append(totalDays + " Days ");
}
if (totalHours > 0)
{
builder.Append(hours + " Hours ");
}
if (totalMinutes > 0)
{
builder.Append(minutes + " Minutes ");
}
builder.Append(seconds + " Seconds");
MessageBox.Show(builder.ToString());
答案 3 :(得分:-1)
TimeSpan是最简单的方法。
int Input = 32453; //Amount of seconds
TimeSpan ts = new TimeSpan(0, 0, Input); //3 constructor arguments are (Hours, Minutes, Seconds)
if (ts.Days > 0)
{
Console.WriteLine(ts.Days + " Day(s)");
}
else if (ts.Hours > 0)
{
Console.WriteLine(ts.Hours + " Hour(s)");
}
else if (ts.Minutes > 0)
{
Console.WriteLine(ts.Minutes + " Minute(s)");
}
else
{
Console.WriteLine(ts.Seconds + " Second(s)");
}
Console.Read();