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?

huangapple go评论96阅读模式
英文:

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"。

注:我知道我的代码看起来很糟糕,因为我是一个纯初学者,不知道如何正确格式化代码。

  1. public class PersonTidbok {
  2. public static void main(String[] args){
  3. Scanner console = new Scanner(System.in);
  4. char choice;
  5. System.out.print("欢迎来到您的界面,请从以下选项中选择:\n" +
  6. "P, T, L, S, A, Q");
  7. choice = console.next().charAt(0);
  8. switch (choice){
  9. case 'P':
  10. do{
  11. System.out.print("请输入格式为 (YYYYMMDD) 的 persnr:");
  12. try{
  13. String persnr = console.next();
  14. if (persnr.length() != 8) {
  15. throw new FormatException();
  16. }
  17. }
  18. catch (FormatException exception){
  19. System.out.println(exception + " 您输入了错误的格式,请重试。");
  20. }
  21. } while (true); // 此处应为循环条件
  22. // 其他选项的处理...
  23. }
  24. }
  25. }

P.S. 请注意,我已经重新格式化了您的代码,并做了一些修改以使其正确运行。在你的代码中,有一些小错误,我已经帮你进行了修正。

英文:

Im trying to tell the user to keep inputing the right value format until its done properly. I want to display to the user that if he/she has typed in the wrong format (number of values), he/she should try again and the system will ask the user to input new values. How do I do that with the following code below?

And is the while statement at the bottom valid (properly written)? Like if the exception isnt triggered, stop "do:ing"

P.S, I know that my code below looks awful, as I'm a mere beginner and do not know how to format code properly

  1. public class PersonTidbok {
  2. public static void main(String[] args){
  3. Scanner console = new Scanner (System.in);
  4. System.out.print ("Welcome to your interface, please select of the following: " +
  5. "\nP, T, L, S, A, Q");
  6. choice = console.next().charAt(0);
  7. switch (choice){
  8. case P:
  9. do{
  10. System.out.print (" enter persnr in the format of (YYYYMMDD));
  11. try{
  12. persnr = console.nextInt();
  13. if ( persnr.length != 8);
  14. throw new FormatException();
  15. }
  16. catch (FormatException exception){
  17. System.out.println(exception + "You printed wrong format, try again"));
  18. }
  19. }
  20. while (!FormatException);
  21. }
  22. }

答案1

得分: 1

  1. import java.util.Scanner;
  2. public class PersonTidbok {
  3. public static void main(String[] argv) {
  4. Scanner console = new Scanner(System.in);
  5. boolean valid;
  6. char choice = '\0';
  7. String persnr;
  8. do {
  9. valid = true;
  10. System.out.print("欢迎访问您的界面,请选择以下操作之一(P,T,L,S,A,Q):");
  11. String input = console.nextLine();
  12. if (input.length() != 1) {
  13. System.out.println("无效输入,请重试。");
  14. valid = false;
  15. }
  16. choice = input.charAt(0);
  17. } while (!valid);
  18. switch (choice) {
  19. case 'P':
  20. do {
  21. valid = true;
  22. System.out.print("请输入格式为(YYYYMMDD)的身份证号码:");
  23. try {
  24. persnr = console.nextLine();
  25. if (!persnr.matches("[1-9]{1}[0-9]{7}")) {
  26. throw new IllegalArgumentException("您输入的格式有误,请重试。");
  27. }
  28. System.out.println("正在处理...");
  29. // ...身份证号码处理应在此处进行
  30. } catch (IllegalArgumentException e) {
  31. System.out.println(e.getMessage());
  32. valid = false;
  33. }
  34. } while (!valid);
  35. break;
  36. default:
  37. System.out.println("选择的值错误。");
  38. }
  39. }
  40. }

示例运行:

  1. 欢迎访问您的界面,请选择以下操作之一(PTLSAQ):a
  2. 选择的值错误。

另一个示例运行:

  1. 欢迎访问您的界面,请选择以下操作之一(PTLSAQ):PT
  2. 无效输入,请重试。
  3. 欢迎访问您的界面,请选择以下操作之一(PTLSAQ):P
  4. 请输入格式为(YYYYMMDD)的身份证号码:01234567
  5. 您输入的格式有误,请重试。
  6. 请输入格式为(YYYYMMDD)的身份证号码:ancdefgh
  7. 您输入的格式有误,请重试。
  8. 请输入格式为(YYYYMMDD)的身份证号码:20180912
  9. 正在处理...
英文:

Do it as follows:

  1. import java.util.Scanner;
  2. public class PersonTidbok {
  3. public static void main(String[] argv) {
  4. Scanner console = new Scanner(System.in);
  5. boolean valid;
  6. char choice = '\0';
  7. String persnr;
  8. do {
  9. valid = true;
  10. System.out.print("Welcome to your interface, please select of the following (P, T, L, S, A, Q): ");
  11. String input = console.nextLine();
  12. if (input.length() != 1) {
  13. System.out.println("Invalid input. Try again.");
  14. valid = false;
  15. }
  16. choice = input.charAt(0);
  17. } while (!valid);
  18. switch (choice) {
  19. case 'P':
  20. do {
  21. valid = true;
  22. System.out.print("Enter persnr in the format of (YYYYMMDD): ");
  23. try {
  24. persnr = console.nextLine();
  25. if (!persnr.matches("[1-9]{1}[0-9]{7}")) {
  26. throw new IllegalArgumentException("You printed wrong format, try again");
  27. }
  28. System.out.println("Processsing...");
  29. // ...Processing of persnr should go here
  30. } catch (IllegalArgumentException e) {
  31. System.out.println(e.getMessage());
  32. valid = false;
  33. }
  34. } while (!valid);
  35. break;
  36. default:
  37. System.out.println("Wrong value for choice.");
  38. }
  39. }
  40. }

A sample run:

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

Another sample run:

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

huangapple
  • 本文由 发表于 2020年3月16日 05:58:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/60698078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定