2017-07-18 22 views
0

我目前是一名軟件測試人員。我有一個場景來測試時間格式。在java中輸入有效的時間格式

我想用java程序輸入有效的時間格式。我累了,但無法得到一個好的答案。

我想以正確的格式輸入時間(hh:mm:ss)。它接受格式HH:MM:SS指定的時間。時間應該用冒號和奇怪冒號隔開,沒有其他字符像/, - 應該打印無效。 小時範圍從0到23分鐘範圍從0到59秒範圍從0到59.

而且只有數字纔會被使用,如果我輸入字符就會打印無效的時間格式。

而且這些2個值20:62:00和20:80:00,也無效,因爲分鐘範圍應爲0〜59

這是我的嘗試:

import java.util.Scanner; 

public class AlarmTime { 
public static void main(String args[]) { 
    Scanner s = new Scanner(System.in); 

    System.out.println("Enter the number of test case"); 
    int a = s.nextInt(); 
    System.out.println("Enter the HH"); 
    int h = s.nextInt(); 
    System.out.println("Enter the MM"); 
    int m = s.nextInt(); 
    System.out.println("Enter the SS"); 
    int ss = s.nextInt(); 
    System.out.println("Time is" + h + ":" + m + ":" + ss); 

    if (h >= 0 && h <= 23 && m >= 0 && m <= 59 && ss >= 0 && ss <= 59) { 
     System.out.println("Valid test case" + h + ":" + m + ":" + ss); 
    } else { 
     System.out.println("invalid test case"); 
    } 
    } 
} 

輸出:

Enter the number of test case 
4 
Enter the HH 
22 
Enter the MM 
33 
Enter the SS 
44 
Time is22:33:44 
Valid test case22:33:44 

Process finished with exit code 0 

在這裏,我寫測試用例爲4,然後再寫如何測試下一個測試用例2.還需要爲無效輸入編寫測試用例,如輸入字符。

+0

我有一個問題。你爲什麼不使用一些測試套件來自動化你的工作?像Junit什麼的。 – Shirkam

+0

你正在使用'||'(或者是)測試,你想'&&'(它是和)。如果(0 <= h <= 23 && 0 <= m <= 59 && 0 <= ss <= 59) –

回答

0

錯誤在這裏,它必須是and條件,而不是or。 小時必須低於24分鐘必須低於60秒必須低於60

if(0<=h<=23 && 0<=m<=59 && 0<=ss<=59){ 
    System.out.println("Valid test case"+h+":"+m+":"+ss); 
} 
else 
{ 
    System.out.println("invalid test case"); 
} 
0

你應該只接受hh:mm:ss而不是把它作爲3個變量,然後試圖解析驗證它它到一個日期。

方法如下:

System.out.println("Enter date as HH:MM:SS"); 
String a = s.nextString(); 
try { 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); 
    Date parsedDate = dateFormat.parse("12-12-2017 "+yourString);   
    // Parsing success, means a valid test case. 
    System.out.println("Valid test case : "+a); 
} catch(Exception e) { 
    // Parsing failed, so the case is invalid! 
    System.out.println("Invalid"); 
} 
3
if(0<=h<=23 ||0<=m<=59||0<=ss<=59) 

不是有效的語法。您不能連鎖<=運營商。

您應該比較一對一的操作數,並在比較之間使用&&運算符。

if(0<=h<=23) 

應該是:

if(0<=h && h<=23) 

無論如何,這不是一條直線和可靠的方式做任務
使用,而一個DateTimeFormatter,而是int,使用String變量將包含有正確的格式輸入:

StringBuilder dateInput = new StringBuilder(); 
System.out.println("Enter the number of test case"); 
int a = dateInput.append(s.next()); 

System.out.println("Enter the HH"); 
dateInput.append(s.next()).append(":"); 

System.out.println("Enter the MM"); 
dateInput.append(s.next()).append(":"); 

System.out.println("Enter the SS"); 
dateInput.append(s.next()); 

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); 

try{ 
    LocalTime time = LocalTime.parse(dateInput.toString(), formatter); 
    System.out.println("Valid"); 
} 
catch(DateTimeParseException e){ 
    System.out.println("Invalid"); 
} 
2

由於其他答案都清楚地指出,您的條件錯了。這是錯誤的去if (hour <= 0)然後打印「有效」。

但超出了那些簡單的句法問題:你在重新發明這裏的車輪。除非這是某種形式的鍛鍊,你被要求寫自己這樣的代碼,您使用的Java日期/時間API的功能:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); 
String str = "13:42:10"; 
LocalTime time = LocalTime.parse(str, formatter); 

parse()調用將拋出一個異常時,傳入的字符串不能被解析。見javadoc。你也想詳細研究可用的patterns

0

neetha。

您的代碼不正確。驗證必須是這樣的:

if (h >= 0 && h <= 23 && m >= 0 && m <= 59 && ss >= 0 && ss <= 59) { 
    System.out.println("Valid test case" + h + ":" + m + ":" + ss); 
} 

無論如何,這將是更好地使用一類像LocalTime爲了管理時間(如果你使用Java 8):

LocalTime time = LocalTime.of(h, m, ss); 

如果時間無效,這段代碼會拋出一個java.time.DateTimeException