我目前是一名軟件測試人員。我有一個場景來測試時間格式。在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.還需要爲無效輸入編寫測試用例,如輸入字符。
我有一個問題。你爲什麼不使用一些測試套件來自動化你的工作?像Junit什麼的。 – Shirkam
你正在使用'||'(或者是)測試,你想'&&'(它是和)。如果(0 <= h <= 23 && 0 <= m <= 59 && 0 <= ss <= 59) –