2017-02-21 73 views
0

我在HackerRank中尝试hallenge /问题如下。 (您不必登录查看问题,对于那些不希望通过链接查看问题的人,我会简要地写一下关于问题的说明)。双因失败的IF逻辑语句c#

https://www.hackerrank.com/challenges/time-conversion

鉴于AM/PM格式日(下午二时34分50秒)的规定输入(串),我不得不将其转换成军用时间(十四点34分50秒)。 Midnight in Military是00:00:00,AM/PM格式是12:00:00 AM,Milatary的下午是12:00:00,AM/PM格式是12:00:00 PM。

现在我使用的方法是检查自指定输入格式以来字符串的第二个值。 B/c我只需要改变小时,我只需要使用“GetNumericValue”将字符串的第0和第1索引转换为双精度。

现在,当我在IF语句(在PM块中)使用这些双精度值时,如果检查“!=”,它们会失败。我已经给出了下面的代码。使用调试器并查看发生了什么后,它完全放弃包含“!=”的IF语句。如果我的输入在其第一个索引中具有“1”或在其第二个索引中具有“2”,则会发生这种情况。

起初我想这可能是一个小数字的double精度问题,但是,如果输入“01:xx:xxPM”,我的代码会给出正确的答案。所以我倾向于认为它是我的IF声明的逻辑,虽然我已经盯着它这么久了,我不知道为什么它不起作用。我给了我编写的代码片段。我基本上是做了我除了用“==”和else语句同样的事情(我用的是前“!=”和else语句)

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

namespace HackerRank_Algorithms 
{ 
    class Time_Conversion 
    { 
     static void Main(string[] args) 
     { 

      //Reading input Date in the format "02:34:50PM" 
      string time = Console.ReadLine(); 
      //Creating a double array for storing the first digits that is the hours 
      //used double since 'GetNumericValue' returns type double 
      double[] timeIn24 = new double[2]; 
      //loop to convert the first two digits from string into double 
      for (int x = 0; x < 2; x++) 
      { 
       timeIn24[x] = char.GetNumericValue(time, x); 
      } 

      //Because I know the date will always be in a specified format I check at the place for 
      //'P' or 'A' for AM or PM 
      //If PM then do this 
      if (time[time.Length-2] == 'P') 
      { 
       //Console.WriteLine("Time is PM"); 

       /******************************************** 
       //I tried doing this since but didn't work 
       //timeIn24[1] = Convert.ToInt32(timeIn24[1]); 
       //timeIn24[0] = Convert.ToInt32(timeIn24[0]); 
       *********************************************/ 

       //Console.WriteLine("Before {0} timeIn24[0]", timeIn24[0]); 
       //Console.WriteLine("Before {0} timeIn24[1]", timeIn24[1]); 


       //PROBLEM OCCURRING HERE I check if its 12PM or not if it isn't then I do the follwoing 
       if (timeIn24[1] != 2 && timeIn24[0] != 1) 
       { 
        //add 2 to the unit of hours 
        timeIn24[1] = timeIn24[1] + 2; 

        //Console.WriteLine("After addition {0} timeIn24[0]", timeIn24[0]); 

        //if the sum i get is greater than 10 that is I'm getting a carry 
        if (timeIn24[1] > 9) 
        { 
         //I add one to the tenth of hours 
         timeIn24[0] = timeIn24[0] + 1; 
         //Modulus to get the last digit of unith hour 
         timeIn24[1] = timeIn24[1] % 10; 
        } 
        timeIn24[0] = timeIn24[0] + 1; 
        Console.Write("{0}{1}", timeIn24[0], timeIn24[1]); 


        //Console.WriteLine(timeIn24[0]); 
        //Console.WriteLine(timeIn24[1]); 
       } 
       else //if its 12PM then I do not do anything and I print it as is 
       { 
        Console.Write("{0}{1}", timeIn24[0], timeIn24[1]); 
       } 

       /***********CODE THAT GAVE THE CORRECT SOLUTION******************/ 
       //if (timeIn24[1] == 2 && timeIn24[0] == 1) 
       //{ 
       // Console.Write("{0}{1}", timeIn24[0], timeIn24[1]); 


       // //timeIn24[0] = timeIn24[0]/10; 
       // //Console.WriteLine(timeIn24[0]); 
       // //Console.WriteLine(timeIn24[1]); 
       //} 
       //else 
       //{ 
       // timeIn24[1] = timeIn24[1] + 2; 
       // //Console.WriteLine("After addition {0} timeIn24[0]", timeIn24[0]); 
       // if (timeIn24[1] > 9) 
       // { 
       //  timeIn24[0] = timeIn24[0] + 1; 
       //  timeIn24[1] = timeIn24[1] % 10; 
       // } 
       // timeIn24[0] = timeIn24[0] + 1; 
       // Console.Write("{0}{1}", timeIn24[0], timeIn24[1]); 
       //} 
       /******************************************************************/ 

       //Here I simply start from the first colon and print the date as string till end 
       int y = 2; 
       while(y < time.Length-2) 
       { 
        Console.Write("{0}", time[y]); 
        y++; 
       } 
      } 
      else //if its AM 
      { 
       //if its 12AM that is Midnight 
       if (timeIn24[0] == 1 && timeIn24[1] == 2) 
       { //set both tenth and unit of hour to 0 
        timeIn24[0] = 0; 
        timeIn24[1] = 0; 
       } 
       //print the hours 
       Console.Write("{0}{1}", timeIn24[0], timeIn24[1]); 

       //again print from first colon + the remaining of string 
       int x = 2; 
       while (x < time.Length-2) 
       { 
        Console.Write("{0}", time[x]); 
        x++; 
       } 
      } 

      Console.ReadLine(); 
     } 
    } 
} 
+1

您需要使用epsilon比较double值,而不只是==或!=或> =或<=等。请参阅:比较Floil和Doubles与Epsilon。 http://stackoverflow.com/questions/2411392/double-epsilon-for-equality-greater-than-less-than-less-than-or-equal-to-gre和http://浮点gui。 de/errors/comparison/ – Brandon

+1

我想你会在'DateTime'类型的.NET中生成更多的成功。 https://msdn.microsoft.com/zh-cn/library/system.datetime(v=vs.110).aspx – maniak1982

+2

有很多冗长的学术课程正在进行。黑客程序是否禁止内置函数?应该有像'TryParse'这样的用户验证。而且你已经有24小时的格式'date.ToString(“HH:mm:ss”)'。 –

回答

0

的问题确实与if声明。考虑使用10:00:00AM作为您的输入。在这种情况下,timeIn24[1] != 2将评估为true(因为第二个数字是0,而不是2),但因为第一个数字是1。确实的第二部分,timeIn24[0] != 1,将评估为false由于您使用&&双方需要评估为true才能输入if声明。

如果使用||(或)而不是&&(和)时,if声明应能正常工作,因为如果要么位不等于正确的值,该代码将进入if块。

当然,和其他人一样,除非特别禁止挑战,否则只需使用内置DateTime类型就简单多了。

+0

啊,你是绝对正确的。我只是看不到它,直到我看到它写在你的答案。谢谢。 –