首页 > 解决方案 > 如何获得正确的时间

问题描述

我正在为一个网站游戏编写时间计算器,我正在玩这个游戏,但是我没有注意到一些关键数字。小时可以设置为 999999...+ 并且它不是以天为单位测量的示例(72 小时不等于 3 天 ingame(它是)它将在 72 小时和分钟、秒、秒之后声明。小时也可以设置为 0,但绝不是 -0 小时。

然而,分钟和秒不能设置为高于 59 和最低 0。但是小数可以设置为最大 999 和最低 0。用户输入示例中不允许负输入 小时:150 分钟:35 秒:40 105 年 12 月。

游戏中的单位将向一个方向移动,然后返回。我的问题是用户输入是 X2,所以 (300:70:80:210.) 这是错误的。秒->分钟->小时。正确的是 301:11:20:210

当它们达到=< 60 或更多时,我如何使分钟变成小时和秒,与其余的相同。(我知道有些代码是不必要的,我一直在测试一些东西)我的问题是:我如何解决时间问题。

我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tribalwars
{
    class Program
    {
        static void Main(string[] args)
        {
            Timer tribalwarsTimer = new Timer();
            
            tribalwarsTimer.TimerSet();
            tribalwarsTimer.Print();
            tribalwarsTimer.Combined();
            Console.ReadKey();
        }
    }
}
using System;

namespace Tribalwars
{
    class Timer
    {
        private int lojal = 100;
        private int lojalChanger = 100;
        private int hour = -1;
        private int minut = -1;
        private int second = -1;
        private double setTime;
        private int _decimal = -1;
        int printHour;
        int printMinut;
        int printSecond;
        int print_Decimal;
        Random randomLojal = new Random();
        // lojal will go upp every 2 60 mins/1hour 
        // lojal drops 20-35 every time an attack happens
        // Time need to be set every attack forward and back
        // make a list for every "TURN" every time noble goes.
        // Tiden går inte om lojalitet är 100
        // The last attack timer is unnecssary. Maybe the first also?
        public Timer()
        {
            Lojal = lojal;
            LojalChanger = lojalChanger;
            SetTime = setTime;
            Minut = minut;
            Hour = hour;
            Second = second;
            _Decimal = _decimal;
        }
        public int _Decimal
        {
            get { return _decimal; }
            set { _decimal = value; }
        }

        public int Second
        {
            get { return second; }
            set { second = value; }
        }
        public int Minut
        {
            get { return minut; }
            set { minut = value; }
        }
        public int Hour
        {
            get { return hour; }
            set { hour = value; }
        }
        public double SetTime
        {
            get { return setTime; }
            set { setTime = value; }
        }
        public int Lojal
        {
            get { return lojal; }
            set { lojal = value; }
        }
        public int LojalChanger
        {
            get { return lojalChanger; }
            set { lojalChanger = value; }
        }
        public int TimerSet()
        {
            do
            {
                Console.WriteLine("Enter how many hours:");
                try{
                    Hour = int.Parse(Console.ReadLine());
                }
                catch(SystemException)
                {
                    Console.WriteLine("Enter a valid number from 0 and higher: ");
                 }
            } while (Hour < 0);
            do
            {

                Console.WriteLine("Enter how many Minuts 0-59:");
                try { Minut = int.Parse(Console.ReadLine()); } 
                catch (SystemException)
                { Console.WriteLine("Enter a valid number from 0 and 59"); }
                
            } while (Minut < 0 || Minut > 59);
            do{
                Console.WriteLine("Enter how many Seconds between 0-59:");
                try { Second = int.Parse(Console.ReadLine()); }
                catch (SystemException)
                { Console.WriteLine("Enter a valid number from 0 and 59"); }

            } while (Second < 0 || Second > 59);

            do
            {
                Console.WriteLine("Enter how many decimals 0-999:");                
                try { _Decimal = int.Parse(Console.ReadLine()); }
                catch (SystemException)
                { Console.WriteLine("Enter a valid number from 0 and 999"); }

            } while (_Decimal < 0 || _Decimal > 999);

            return hour;
        }
        public void BaseTick()
        {

            for (int i = 0; i < Lojal; i++)
            {
                Console.WriteLine("Lojal is set now at {0}", i + 1);
            }
        }

        public int LojaltyDrop()
        {
            do {Lojal = randomLojal.Next(20, 35); }
            while (Lojal < 0);
            lojalChanger -= lojal;
           
            return lojalChanger;
        }
        public void Combined()
        {
            if (LojalChanger >= 0)
            {
                for (int i = 0; i < lojal; i++)
                {

                    LojaltyDrop();
                    Console.WriteLine(lojalChanger);                
                    lojalChanger += Hour + Hour;
                    Console.WriteLine("After hour added {0}",lojalChanger);
                    Console.ReadLine();
                }
            }
            Console.WriteLine("Nice job");
        }
        public void Print()
        {
            printHour += Hour + Hour;
            printMinut += Minut + Minut;
            printSecond += Second + Second;
            print_Decimal += _Decimal + _Decimal;
            Console.WriteLine("In {0}:{1}:{2}:{3}", printHour, printMinut, printSecond, print_Decimal);
        }

    }
}

标签: c#mathtime

解决方案


推荐阅读