java - 如何防止 do while 循环重置?

标签 java exception input java.util.scanner do-while

Fig.1

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;
}
}   

最佳答案

多个 do while 循环在这里效果更好。

如果您在获取时间时开始执行操作,并在完成后对其进行变量检查,则每个其他阶段都有一个单独的检查。

将所有这些包装在一个大的 do while 循环中。这样,当您处于大型 do 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

显然您也有异常测试,只是为了向您展示一般结构。可以存储值并检查它们是否为空,而不是使用 boolean 值来检查。

关于java - 如何防止 do while 循环重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50215900/

相关文章:

java - 如何计算下一个具有 ip 位表示形式的 IP 地址?

java - 在 Java 中使用循环创建金字塔

Java 安全异常

jquery - 自动调整圆 Angular 输入框的大小?

javascript - 如何制作单选和复选框的组合?

java - 计算文本文件中的行数、单词数和字符数

java - 尽管有 xml,但仍因缺少 ListView 而收到 RuntimeException

java - 覆盖所有方法并自动传递第一个参数的装饰器

java - 我想从文本文件添加值并将其添加到二维java中。但给出空指针异常

java - 如何在 Eclipse 中使用测试驱动设计概念测试异常