如何在执行后终止do-while循环?

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

How can I terminate a do-while loop after execution?

问题

我不确定在这段代码中哪个语句应该用(choice!=o);来替换:

  1. choice 是一个 char 类型,用于 switch case 语句中的 Y、y、N、n。
  2. checkEmployment()evaluate() 运行结束后终止,或者在调用 default 后终止。
  1. do {
  2. System.out.println("你是否就业?");
  3. System.out.println("[Y] 我已就业。");
  4. System.out.println("[N] 我未就业。");
  5. choice = inputInfo.next().charAt(0);
  6. switch(choice) {
  7. case 'Y': checkEmployment();
  8. break;
  9. case 'y': checkEmployment();
  10. break;
  11. case 'N': evaluate();
  12. break;
  13. case 'n': evaluate();
  14. break;
  15. default: System.out.println("无效的输入。程序终止。");
  16. break;
  17. } // 这是就业状态的条件语句。
  18. } while(choice != 'o'); // 这是就业状态的 do-while 循环语句。

我正在学习,任何帮助都将不胜感激。干杯。

英文:

I’m not sure what statement should be replaced with (choice!=o); in this code:

  1. choice is a char for the switch case Y,y,N,n.
  2. Terminate after checkEmployment() and evaluate() is finished running, or if default is invoked
  1. do {
  2. System.out.println("Are you employed?");
  3. System.out.println("[Y] I am employed.");
  4. System.out.println("[N] I am unemployed.");
  5. choice = inputInfo.next().charAt(0);
  6. switch(choice) {
  7. case 'Y': checkEmployment();
  8. break;
  9. case 'y': checkEmployment();
  10. break;
  11. case 'N': evaluate();
  12. break;
  13. case 'n': evaluate();
  14. break;
  15. default: System.out.println("Invalid input. Terminating Program");
  16. break;
  17. }//This is the conditional for the employment status.
  18. } while(choice!='o'); //This is the do-while statement for the employment status.

I’m learning and any help would be appreciated. Cheers.

答案1

得分: 1

你可以通过移除两个break语句来简化switch语句。然后,我会添加一个布尔变量来跟踪有效输入。将其用作while循环的条件。

  1. boolean validInput = false;
  2. do {
  3. System.out.println("你是否就业?");
  4. System.out.println("[Y] 我已就业。");
  5. System.out.println("[N] 我未就业。");
  6. choice = inputInfo.next().charAt(0);
  7. switch(choice) {
  8. case 'Y':
  9. case 'y':
  10. validInput = true;
  11. checkEmployment();
  12. break;
  13. case 'N':
  14. case 'n':
  15. validInput = true;
  16. evaluate();
  17. break;
  18. default:
  19. System.out.println("错误 - 无效输入");
  20. break;
  21. }
  22. } while (!validInput);
英文:

You can tidy the switch statement up a bit by getting rid of two of the breaks. Then I would add a boolean to keep track of valid input. Use that as your while condition.

  1. boolean validInput = false;
  2. do {
  3. System.out.println("Are you employed?");
  4. System.out.println("[Y] I am employed.");
  5. System.out.println("[N] I am unemployed.");
  6. choice = inputInfo.next().charAt(0);
  7. switch(choice) {
  8. case 'Y':
  9. case 'y':
  10. validInput = true;
  11. checkEmployment();
  12. break;
  13. case 'N':
  14. case 'n':
  15. validInput = true;
  16. evaluate();
  17. break;
  18. default:
  19. System.out.println("Error - invalid input");
  20. break;
  21. }
  22. } while (!validInput);

huangapple
  • 本文由 发表于 2020年9月30日 16:33:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64133828.html
匿名

发表评论

匿名网友

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

确定