2017-02-21 76 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

啊,你是絕對正確的。我只是看不到它,直到我看到它寫在你的答案。謝謝。 –