C#:秒到几分钟到几小时的转换? [英] C# : Seconds to Minutes to Hours conversion?

查看:638
本文介绍了C#:秒到几分钟到几小时的转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了第一关,我敢肯定,我不希望使用时间跨度为这项任务;而一个公式一系列显示秒,分,并在消息框中小时,当用户输入的文本框中的秒数。



这里就是我卡住。我们应该检查我们的答案的例子:7565秒为2小时,6分,5秒。然而,我的代码最终计算作为2个小时,6分钟,和6秒。这也使这个问题的答案时,最初数量为7560秒。我很困惑!!它是一个条件的情况下,在该消息框显示,只有当用户在60秒进入秒,如果只分钟+秒用户输入秒60和3600之间,和小时±分钟±秒如果输入超过3600秒。这里是我到目前为止,我会很感激任何见解,为什么我的计算是关闭的:)



感谢您的答案!但7565不是一个常数;用户可以输入任何秒金额,但我的教授用7565为例来检查,如果我们在正确的轨道上。

 私人无效calculateButton1_Click(对象发件人,EventArgs五)
{
INT totalSeconds,小时,分钟,minutesRemainder,hoursRemainderMinutes,hoursRemainderSeconds;
totalSeconds = int.Parse(secondsTextBox1.Text);
=分钟totalSeconds / 60;
minutesRemainder = totalSeconds%60;
小时数=分钟/ 60;
hoursRemainderMinutes =分钟%60;
hoursRemainderSeconds = hoursRemainderMinutes%60;

如果(totalSeconds&下; 60)
{
MessageBox.Show(totalSeconds.ToString());
}

如果其他人(totalSeconds< 3600)
{

MessageBox.Show(minutes.ToString()+分钟+ minutesRemainder的ToString()+秒);
}

如果其他人(totalSeconds> 3600)
{
MessageBox.Show(hours.ToString()+小时+ hoursRemainderMinutes.ToString()+ 分,+ hoursRemainderSeconds.ToString()+秒);
}

}


解决方案

尝试使用的模块的算术

  INT totalSeconds = 7565; 

INT小时= totalSeconds / 3600;
INT分钟=(totalSeconds%3600)/ 60;
INT秒=(totalSeconds 60%);



...

 如果(时间大于0)
MessageBox.Show(的String.Format({0}小时{1}分钟,{2}秒,时,分,秒));
否则如果(分钟大于0)
MessageBox.Show(的String.Format({0}分钟,{1}秒,分,秒));
,否则
MessageBox.Show(的String.Format({0}秒秒));


Okay first off, I'm pretty sure I'm not expected to use TimeSpan for this assignment; rather a formula series which shows the seconds, minutes, and hours in a message box when the user enters the number of seconds in the text box.

Here's where I'm stuck. We're supposed to check our answers with the example: 7565 seconds is 2 hours, 6 minutes, and 5 seconds. However, my code ends up calculating it as 2 hours, 6 minutes, and 6 seconds. It also keeps that answer when the initial number is 7560 seconds. I'm so confused!! It's a conditional scenario, in which the message box shows only the seconds if the user enters under 60 seconds, only minutes + seconds if the user enters between 60 and 3600 seconds, and hours + minutes + seconds if over 3600 seconds is entered. Here is what I have so far, and I'd appreciate any insight as to why my calculation is off :)

Thanks for the answers! But the 7565 isn't a constant; the user can enter any amount of seconds but my professor used 7565 as an example to check if we're on the right track.

private void calculateButton1_Click(object sender, EventArgs e)
    {
        int totalSeconds, hours, minutes, minutesRemainder, hoursRemainderMinutes, hoursRemainderSeconds;
        totalSeconds = int.Parse(secondsTextBox1.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>3600)
        {
            MessageBox.Show(hours.ToString() + " hours, " + hoursRemainderMinutes.ToString() + " minutes, " + hoursRemainderSeconds.ToString() + " seconds");
        }

    }

解决方案

Try using modular arithmetics

int totalSeconds = 7565;

int hours = totalSeconds / 3600;
int minutes = (totalSeconds % 3600) / 60;
int seconds = (totalSeconds % 60);

...

if (hours > 0)
  MessageBox.Show(String.Format("{0} hours, {1} minutes, {2} seconds", hours, minutes, seconds));
else if (minutes > 0)
  MessageBox.Show(String.Format("{0} minutes, {1} seconds", minutes, seconds));
else
  MessageBox.Show(String.Format("{0} seconds", seconds));

这篇关于C#:秒到几分钟到几小时的转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆