我怀疑我的问题出在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;
}
}
答案 0 :(得分:0)
声明整数小时,整数分钟..
在提供属性时存储您的输入,例如this.hours = getHours();
测试您的属性是否已定义,例如if(this.hours == null){ this.hours = getHours()}
答案 1 :(得分:0)
多个while while循环在这里会更好用。
如果您在上班时间开始做,并在完成时进行变量检查,则为每个其他阶段单独进行检查。
将所有这些包装在一个大的while循环中。这样,当你处于大做while循环时,它将检查每个阶段是否完成。下面的小sudo代码示例:
bool hours = false
bool min = false
bool seconds = false
Do:
Do(
getHours()
While bool hours = false)
Do(
getmin()
While bool min= false)
Do(
getseconds()
While bool seconds= false)
while ok == false
显然,您也有例外测试,只是为了向您展示一般结构。可以存储值并检查它们是否也为空,而不是检查bool。