簡體   English   中英

如何在 try catch 語句的幫助下創建一個循環,命令用戶繼續輸入一個值,直到滿足條件為止?

[英]How do I make a loop that commands the user to keep inputing a value until If conditions are met, with the aid of a try catch statement?

我試圖告訴用戶繼續輸入正確的值格式,直到正確完成。 我想向用戶顯示,如果他/她輸入了錯誤的格式(值的數量),他/她應該再試一次,系統將要求用戶輸入新值。 我如何使用下面的代碼來做到這一點?

底部的 while 語句是否有效(正確編寫)? 就像沒有觸發異常一樣,停止“do:ing”

PS,我知道我下面的代碼看起來很糟糕,因為我只是一個初學者,不知道如何正確格式化代碼

public class PersonTidbok {

   public static void main(String[] args){


      Scanner console = new Scanner (System.in);

      System.out.print ("Welcome to your interface, please select of the following: " +
                        "\nP, T, L, S, A, Q"); 

  choice = console.next().charAt(0);

      switch (choice){

 case P:

      do{ 

      System.out.print (" enter persnr in the format of (YYYYMMDD));

        try{

        persnr = console.nextInt();

           if ( persnr.length != 8);

             throw new FormatException();
      }

                catch (FormatException exception){
                System.out.println(exception + "You printed wrong format, try again")); 
   }
}
   while (!FormatException);
}
}

請按以下步驟操作:

import java.util.Scanner;

public class PersonTidbok {
    public static void main(String[] argv) {
        Scanner console = new Scanner(System.in);
        boolean valid;
        char choice = '\0';
        String persnr;
        do {
            valid = true;
            System.out.print("Welcome to your interface, please select of the following (P, T, L, S, A, Q): ");
            String input = console.nextLine();
            if (input.length() != 1) {
                System.out.println("Invalid input. Try again.");
                valid = false;
            }
            choice = input.charAt(0);
        } while (!valid);

        switch (choice) {
        case 'P':
            do {
                valid = true;
                System.out.print("Enter persnr in the format of (YYYYMMDD): ");
                try {
                    persnr = console.nextLine();
                    if (!persnr.matches("[1-9]{1}[0-9]{7}")) {
                        throw new IllegalArgumentException("You printed wrong format, try again");
                    }
                    System.out.println("Processsing...");
                    // ...Processing of persnr should go here
                } catch (IllegalArgumentException e) {
                    System.out.println(e.getMessage());
                    valid = false;
                }
            } while (!valid);
            break;
        default:
            System.out.println("Wrong value for choice.");
        }
    }
}

示例運行:

Welcome to your interface, please select of the following (P, T, L, S, A, Q): a
Wrong value for choice.

另一個示例運行:

Welcome to your interface, please select of the following (P, T, L, S, A, Q): PT
Invalid input. Try again.
Welcome to your interface, please select of the following (P, T, L, S, A, Q): P
Enter persnr in the format of (YYYYMMDD): 01234567
You printed wrong format, try again
Enter persnr in the format of (YYYYMMDD): ancdefgh
You printed wrong format, try again
Enter persnr in the format of (YYYYMMDD): 20180912
Processsing...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM