猿问

如何在for循环中增加小时

对于学校练习,我必须在WPF应用程序中创建一个文本时钟,该时钟将为您提供文本时间。例如,我想要的是:当它是10:30时,时钟应该说是十点半,荷兰语是“ 11点半之前”,“半11点”。但是我不确定如何在我尝试过的for循环中做到这一点,i++;但这并没有增加小时。


请记住,我刚刚开始编写代码,所以我在这两种方法中都可能犯过很多错误。


希望对每个人都清楚,否则我将所有内容都更改为英语。


public partial class MainWindow : Window

{

    public MainWindow()

    {

        InitializeComponent();


        var date = DateTime.Now;


        TijdHet.Foreground = new SolidColorBrush(Colors.Red);

        TijdIs.Foreground = new SolidColorBrush(Colors.Red);


        var time = new Label[]

        {

            TijdEen, TijdTwee, TijdDrie, TijdVier, TijdVijf, TijdZes, TijdZeven, TijdAcht, TijdNegen, TijdTien,

            TijdElf, TijdTwaalf

        };


        int GetMinutes() {

            var minutes = 5 * (int) Math.Round(date.Minute / 5.0);

            return minutes;

        }


        int GetHour()

        {

            var hour = (date.Hour + 11) % 12 + 1;


            if (hour == 1) {

                TijdEen.Foreground = new SolidColorBrush(Colors.Red);

            }


            return hour;

        }


        for (int i = 1; i <= 12; i++)

        {

            if (GetHour() == i + 1)

            {

                time[i].Foreground = new SolidColorBrush(Colors.Red);


                if (GetMinutes() == 0) {

                    TijdUur.Foreground = new SolidColorBrush(Colors.Red);

                }


                if (GetMinutes() == 5 || GetMinutes() == 10 || GetMinutes() == 35 || GetMinutes() == 40) {

                    TijdOver.Foreground = new SolidColorBrush(Colors.Red);

                }


                if (GetMinutes() == 20 || GetMinutes() == 25 || GetMinutes() == 50 || GetMinutes() == 55) {

                    TijdVoor.Foreground = new SolidColorBrush(Colors.Red);

                }


                if (GetMinutes() == 30)  {

                    TijdHalf.Foreground = new SolidColorBrush(Colors.Red);

                    i++;

                }

            }

        }

    }

}



呼如林
浏览 205回答 3
3回答

月关宝盒

这只是伪代码,但将向您显示增加小时数所需的操作var&nbsp;dt&nbsp;=&nbsp;DateTime.Now.AddHours(1);因此,在您的代码中,您只需要做date.AddHours(1);希望这应该给你足够的继续

万千封印

您的代码很难按照控件上的颜色进行设置。这是一些可以像人类所说的那样打印时间的代码。我希望您可以调整它以设置控件上的颜色。static void PrintTime(int hours, int minutes){&nbsp; &nbsp; if (minutes >= 30)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hours++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;minutes -= 60;&nbsp; &nbsp; }&nbsp; &nbsp; hours = hours % 12;&nbsp; &nbsp; if (hours == 0) hours = 12;&nbsp; &nbsp; Console.WriteLine("The time is " + Math.Abs(minutes) + (minutes < 0 ? " to " : " past ") + hours);}如果你这样称呼它PrintTime(10, 29);PrintTime(10, 30);PrintTime(10, 31);它会打印The time is 29 past 10The time is 30 to 11The time is 29 to 11
随时随地看视频慕课网APP
我要回答