秒转换成分钟,小时和天

本文关键字:小时 分钟 转换 | 更新日期: 2023-09-27 18:06:16

我们最近在课上讨论了"if语句等",我在这个问题上遇到了麻烦。(抱歉我的英语很差)

问题是:

  • 创建一个允许用户输入秒数的应用程序,其工作方式如下:
  • 一分钟有60秒。如果用户输入的秒数大于或等于60时,程序应显示多少分钟
  • 一小时有3600秒。输入的秒数用户大于等于3600时,程序应显示该数字
  • 一天有86400秒。如果用户输入的秒数大于或等于86,400时,程序应显示的数字是

这是我错误百出的回答:

    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");

        }
    }
}

}

当运行时,我的程序不计算任何东西。我做错了什么?

秒转换成分钟,小时和天

您应该使用TimeSpan。FromSeconds方法。

它会给你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
}

好的。假设您不想使用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());
}

时间跨度是最简单的方法。

        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();

如何计算所有值并确定哪些值需要在输出

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());