简体   繁体   中英

time converter C#

using System;

namespace timezones
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("What hour is it?");
            //what time is it in Switzerland?
            Byte TimeSwitzerland = Convert.ToByte(Console.ReadLine());
            //time in Scottsdale is 8 hours in behind
            var TimeScottsdale = TimeSwitzerland - 8;
            Console.WriteLine("in which minute(s)?");
            Byte MinuteSwitzerland = Byte.Parse(Console.ReadLine());
            var count = MinuteSwitzerland.ToString().Length;
            //string MinutestringSwitzerland = MinuteSwitzerland.ToString();
            Byte[] digits = new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Byte[] ZeroDigits = new Byte[] { 0, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09 };
            float Minutes() { 
            while (count != 2 )
            {
                for (int i=0; i < digits.Length; i++)
                {
                    if (MinuteSwitzerland == Convert.ToByte(i))
                    {
                        MinuteSwitzerland = ZeroDigits[i];
                        continue;
                        
                    }
                }
            }
                return MinuteSwitzerland;
            }
            if (TimeScottsdale < 0)
            {
                TimeScottsdale = 24 + TimeScottsdale;
                Console.WriteLine("it is " + TimeScottsdale + ":" + Minutes() + " in Scottsdale right now");
            }
            else
            {
                Console.WriteLine("it is " + TimeScottsdale + ":" + Minutes() + " in Scottsdale right now");
            }

        }
    }
}

So, I'm trying to code a time converter for switzerland to arizona, however it returns the correct time if you use something like 5:32 but it doesnt if you use 5 for hours and something from 0-9 for minutes. I could just ignore the fact, that if you put a diget for minutes it says something like 5:2 instead of 5:02. But i want it to look good and work properly (I might have to work on the asthetics of my code, I know that things like using var instead of defining the datatype is not that nice:) Considering it works with 2 digets (if you put one diget for minutes the programm just stopps and returns nothing), the problem has either to be inside of the Minute method or the while (count.= 2){} loop? Could someone help me out finding the problem?

Change this line: var count = MinuteSwitzerland.ToString().Length; to var count = MinuteSwitzerland.ToString("00").Length;

using System;

namespace timezones
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("What hour is it?");
            //what time is it in Switzerland?
            Byte TimeSwitzerland = Convert.ToByte(Console.ReadLine());
            //time in Scottsdale is 8 hours in behind
            var TimeScottsdale = TimeSwitzerland - 8;
            Console.WriteLine("in which minute(s)?");
            Byte MinuteSwitzerland = Byte.Parse(Console.ReadLine());
            var count = MinuteSwitzerland.ToString("00").Length;
            //string MinutestringSwitzerland = MinuteSwitzerland.ToString();
            Byte[] digits = new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Byte[] ZeroDigits = new Byte[] { 0, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09 };
            String Minutes() { 
            while (count != 2 )
            {
                for (int i=0; i < digits.Length; i++)
                {
                    if (MinuteSwitzerland == Convert.ToByte(i))
                    {
                        MinuteSwitzerland = ZeroDigits[i];
                        continue;
                        
                    }
                }
            }
                return MinuteSwitzerland.ToString("00");
            }
            if (TimeScottsdale < 0)
            {
                TimeScottsdale = 24 + TimeScottsdale;
                Console.WriteLine("it is " + TimeScottsdale + ":" + Minutes() + " in Scottsdale right now");
            }
            else
            {
                Console.WriteLine("it is " + TimeScottsdale + ":" + Minutes() + " in Scottsdale right now");
            }

        }
    }
}

Now it works:) huge thanks to Caius Jard and zir

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM