* Рис.1 1002 *
Fig.2
Я подозреваю, что моя проблема в цикле do while. Пожалуйста, используйте мой код и изображения для справки.
Мне удалось заставить программу принимать только правильное значение ввода, даже если вы введете неправильное значение, как вы можете видеть на рис.1.
Но он всегда будет возвращаться к первому входу, когда вы введете неправильное значение на втором или третьем входе. Пожалуйста, см. Рис.2 для вывода, который я ищу.
import java.util.Scanner;
public class Lab4 {
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
boolean ok = true;
do {
try
{
int hours = getHours();
int minutes = getMinutes();
int seconds = getSeconds();
print24HourTime(hours, minutes, seconds);
ok = true;
}
catch (InvalidHrExcep a)
{
System.out.println (a.getmessage());
ok = false;
}
catch (InvalidMinExcep b)
{
System.out.println(b.getmessage());
ok = false;
}
catch (InvalidSecExcep c)
{
System.out.println (c.getmessage());
ok = false;
}
}while (ok == false);
}
public static int getHours() throws InvalidHrExcep
{
Scanner input = new Scanner (System.in);
System.out.println("Enter hours: ");
int hoursValue = input.nextInt();
if ((hoursValue < 0 ) || (hoursValue > 12)) {
throw new InvalidHrExcep("The value of hours must be between 0 and 12");
}
return hoursValue;
}
public static int getMinutes() throws InvalidMinExcep
{
Scanner input = new Scanner (System.in);
System.out.println("Enter minutes: ");
int minutesValue = input.nextInt();
if ((minutesValue <0) || (minutesValue > 60))
{
throw new InvalidMinExcep("The value of minutes must be between 0 and 60");
}
return minutesValue;
}
public static int getSeconds() throws InvalidSecExcep
{
Scanner input = new Scanner (System.in);
System.out.println("Enter seconds: ");
int secondsValue = input.nextInt();
if ((secondsValue <0) || (secondsValue > 60))
{
throw new InvalidSecExcep("The value of seconds must be between 0 and 60");
}
return secondsValue;
}
public static void print24HourTime(int hr, int min, int sec)
{
Scanner input = new Scanner (System.in);
System.out.println("Enter AM or PM: ");
String ampm = input.next();
if ((ampm.equals("am") || (ampm.equals("AM"))))
{
System.out.printf("24 hour clock time: %02d:%02d:%02d" , hr, min, sec);
}
if ((ampm.equals("pm") || (ampm.equals("PM"))))
{
System.out.printf("24 hour clock time: %02d:%02d:%02d" , hr+12, min, sec);
}
}
}
class InvalidSecExcep extends Exception{
private String message;
public InvalidSecExcep()
{
}
public InvalidSecExcep(String str)
{
this.message = str;
}
public String getmessage()
{
return this.message;
}
public String tostring()
{
String c = message;
return c;
}
}
class InvalidMinExcep extends Exception {
private String message;
public InvalidMinExcep()
{
}
public InvalidMinExcep(String str)
{
this.message = str;
}
public String getmessage()
{
return this.message;
}
public String tostring()
{
String b = message;
return b;
}
}
class InvalidHrExcep extends Exception{
private String message;
public InvalidHrExcep()
{
}
public InvalidHrExcep(String str)
{
this.message = str;
}
public String getmessage()
{
return this.message;
}
public String tostring()
{
String a = message;
return a;
}
}