2013-08-12 2 views
0

기존의 것을 대체하는 가장 작은 시간을 저장하는 방법이 필요하지만 현재 [아래]가 시도하지 않은 것이 작동하지 않으며 때로는 2 : 38.4가 2보다 작다고 말할 수 있습니다. 20.1. 올바른 경로로 쓰기 형태로 3 텍스트 상자작은 시간을 결정하는 방법 C#

timerMin 
timerSec 
timerMil 

에서 텍스트 파일

88:88:8 

에서

.

   using (TextReader reader = File.OpenText(pathPlayer + player[id].name + "\\time.txt")) 
       { 
        string z = reader.ReadLine(); 
        string[] zsplit = z.Split(':'); 
        reader.Close(); 
        fileMin = Convert.ToInt32(timerMinute.Text); 
        recMin = Convert.ToInt32(zsplit[0]); 
        if (fileMin < recMin) 
        { 
         File.WriteAllText(pathPlayer + player[id].name + "\\time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text); 
         newPersonalRecord = true; 
        } 
        else 
        { 
         fileSec = Convert.ToInt32(timerSecond.Text); 
         recSec = Convert.ToInt32(zsplit[1]); 
         if (fileSec < recSec) 
         { 
          File.WriteAllText(pathPlayer + player[id].name + "\\time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text); 
          newPersonalRecord = true; 
         } 
         else 
         { 
          fileMil = Convert.ToInt32(timerMili.Text); 
          recMil = Convert.ToInt32(zsplit[1]); 
          if (fileMil < recMil) 
          { 
           File.WriteAllText(pathPlayer + player[id].name + "\\time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text); 
           newPersonalRecord = true; 
          } 
          else 
          { 

          } 
         } 
        } 

       } 

나는 꽤 오랫동안이 작업을 한 내가 잘못 갈 어디서 볼 수 있고, 도움이 뛰어난 것입니다.

감사

+0

Timespan에는 구문 분석 메서드가 있습니다.이 매개 변수를 사용하여 timespans를 구문 분석하고 비교합니다. – Bearcat9425

+0

_ 텍스트 파일 '88 : 88 : 8'_ -에서 TimeSpan으로 구문 분석하지 않습니다. 그게 문자 그대로 야? –

답변

2
당신이 타임 스팬

을 비교해야 할 때 파일의 문자열을하지 않으면 당신이 텍스트 상자를 비교하는

최대 "23시 59분 59초"(하루의 시간을 초과) 당신은 TimeSpan.Parse("18:44:08");을 수행하여 타임 스팬을 만들기 위해 문자열을 사용하고

   fileMin = new TimeSpan(0, 0, int.Parse(timerMin), int.Parse(timerSec), int.Parse(timerMil)); 
       recTimeSpan = TimeSpan.Parse(z); 

       if(fileMin > recTimeSpan) 
       {// Your code} 
       else 
       {// Your code} 

당신이 항상 할 수

012처럼 그들을 비교할 수 있습니다
+0

시간대는 무엇입니까? OP는 텍스트 상자의 정수를 파일의 정수 (각 부분)와 비교한다고 생각합니다. 어느 것이 좋을까요? 또한 시간은시, 분, 초가 아닙니다. 분, 초, 밀리 초입니다. 나 같은 질문을 읽고 계신가요? – musefan

+0

@musefan 그는 파일에 "88 : 88 : 8"과 같은 문자열이 있다고 말합니다. –

+0

그래, 88 분인가요? 아니요, 그 값은 미정의 형식을 나타내는 것으로 가정해야합니다. 여기서 '8'은 숫자를 나타냅니다. 그런 다음 영업 담당자는 자신의 변수 구성으로 인해 그의 세 구성 요소가 분, 초 및 밀리 초임을 명확하게 보여줍니다. – musefan

0

코드에는 제거해야 할 반복 사항이 많이 있습니다. TimeSpan을 사용하여 값을 저장하고 비교할 수도 있습니다. 이 같은

시도 뭔가 :

string filePath = pathPlayer + player[id].name + "\\time.txt"; 
string z = null; 
using (TextReader reader = File.OpenText(filePath)) 
{ 
    z = reader.ReadLine(); 
} 
string[] zsplit = z.Split(':'); 

//Create a timespan from the values read from the .txt file 
TimeSpan fileTime = new TimeSpan(0, 0, int.parse(zsplit[0]), int.parse(zsplit[1]), int.parse(zsplit[2])); 
//Create a timespan from the values stored in the Textbox controls 
TimeSpan inputTime = new TimeSpan(0, 0, int.parse(timerMinute.Text), int.parse(timerSecond.Text), int.parse(timerMili.Text)); 

//Check if the new TextBox time is faster than the time stored in the file 
if(inputTime < fileTime) 
{ 
    //new faster time, so update the file 
    File.WriteAllText(filePath, inputTime.ToString("mm:ss:f")); 
    newPersonalRecord = true; 
} 

몇 가지주의해야 할 : 입력에 대한 유효성 검사가 없습니다

  • , 그래서 유효하지 않은 데이터가 응용 프로그램을 충돌합니다 (유효성 검사를 추가해야 참조 : int.TryParse). 또한 z가 null는 아니고, 유효성을 검사 할 것이며, zsplit은이어야 3fileMinrecMin의 당신의 이름은 당신의 데이터 소스와 일치하지 않았다, 나는 fileTimeinputTime로 이름이 변경
  • 될 더 명확
  • Length있다 그런 다음 바로 if 문에 주위를 전환 그것을 다른 방법을 원하는 경우 inputTime 경우
  • 이 예제 검사, fileTime 미만이 예에서는 TimeSpan의 사용이 분 구성 요소가 될 수 없다는 가정
  • 이상 59
1

이것은 디자인 퀴프에 더 가깝지만 정수가 아닌 TimeSpan 클래스를 사용하는 것이 좋습니다. 코드 작성 및 읽기가 훨씬 쉬워집니다. 검색하는 시간 데이터에서 새 TimeSpan을 생성 한 다음 하나의 비교 연산자를 사용하여 분당 초 및 밀리 초가 아닌 기존 레코드보다 크거나 작음을 판단 할 수 있습니다.

당신은 예를 들어

(단지 빼기 연산자를 사용하여 매우 쉽게 당신이 시간 범위로 날짜 시간의 차이를 찾을 수 있습니다) 시작 및 종료 시간을 추적하는 데 사용하는 경우

DateTime도 일할 수 :

DateTime StartTime 
DateTime EndTime 
TimeSpan difference = EndTime = StartTime 
관련 문제