どのように私は条件がトライcatch文の助けを借りて、満たされている場合まで値を入力環境維持するために、ユーザーがコマンドループを作るのですか?

Zerenity:

イムは、適切に行われるまで、右の値のフォーマットを入力環境保つためにユーザーに伝えるためにしようとしています。私は彼/彼女が間違った形式(値の数)で入力した場合、彼/彼女はもう一度試してみてください、システムは新しい値を入力するユーザーを要求されますことをユーザーに表示したいです。私は、以下の次のコードでそれをどのように行うのですか?

そして、(適切に書かれた)下有効にwhile文のですか?例外イマイチがトリガ場合と同様に、停止は「やる: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);
}
}
アービンド・クマールのAvinash:

次のように実行します。

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...

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=292861&siteId=1