javaで時刻を入力するフォーマットはどのようなものですか
Java では、Scanner クラスを使用してユーザーの時間入力を取得できます。以下に、ユーザー入力時刻の取得方法を示すサンプルコードフラグメントを示します。
import java.util.Scanner;
public class TimeInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入时间(格式为HH:mm:ss):");
String timeString = scanner.nextLine();
try {
String[] timeParts = timeString.split(":");
int hours = Integer.parseInt(timeParts[0]);
int minutes = Integer.parseInt(timeParts[1]);
int seconds = Integer.parseInt(timeParts[2]);
System.out.println("您输入的时间是:" + hours + "时" + minutes + "分" + seconds + "秒");
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
System.out.println("输入的时间格式不正确");
}
scanner.close();
}
}
上記のコードを実行すると、ユーザーは時間を入力するように求められ、”時:分:秒”の形式で入力する必要があります。その後、プログラムはユーザーが入力した時間を解析し、各時間部分の値を出力します。
実際のアプリケーションでは、ユーザ入力の時間が適切なフォーマットであることを保証するために、さらなる検証とエラー処理ロジックを追加する必要があります。