How can i repeat statement "enter marks or grade" with in while loop.In this case every time control back to start of loop to enter subject again?

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

How can i repeat statement "enter marks or grade" with in while loop.In this case every time control back to start of loop to enter subject again?

问题

  1. public class Marks2 {
  2. public static void main(String args[]){
  3. Scanner s=new Scanner(System.in);
  4. System.out.println("Here is Subject list");
  5. System.out.println("1-Physics");
  6. System.out.println("2-Math");
  7. System.out.println("3-computer");
  8. boolean input=true;
  9. while (input){
  10. System.out.println("Enter subject number here");
  11. String sub=s.nextLine();
  12. if(sub.equals("1")) {
  13. System.out.println("Physics");
  14. System.out.println("");
  15. System.out.println("Enter marks /Enter change for change /Enter exit for leave");
  16. String change1 = null;
  17. String change2 = null;
  18. int marks = 0;
  19. try {
  20. change1 = s.nextLine();
  21. marks = Integer.parseInt(change1);
  22. } catch (Exception e) {
  23. System.out.println("Please enter only string value");
  24. change2 = s.nextLine();
  25. if (change2.equals("change")) {
  26. continue;
  27. } else if (change2.equals("exit")) {
  28. System.exit(0);
  29. }
  30. }
  31. if(marks<40){
  32. System.out.println("Student is fail");
  33. }
  34. else if(marks==40){
  35. System.out.println("Student is fail he need more practice");
  36. }
  37. else if(marks<70){
  38. System.out.println("need more practice but also good");
  39. }
  40. else if(marks==70){
  41. System.out.println("Good");
  42. }
  43. else if(marks<90){
  44. System.out.println("Good but also Excellent");
  45. }
  46. else if(marks==90){
  47. System.out.println("Excellent");
  48. }
  49. else if(marks<100){
  50. System.out.println("Outstanding");
  51. }
  52. else if(marks==100){
  53. System.out.println("Good but also excellent");
  54. }
  55. else if(change1.equals("change")){
  56. continue;
  57. }
  58. else if(change2.equals("exit")){
  59. System.exit(0);
  60. }
  61. else {
  62. System.out.println("");
  63. }
  64. continue;
  65. }
  66. }
  67. }
  68. }

继续控制会回到循环开始,并提示用户重新输入科目。是否有可能允许用户输入分数,直到用户想要离开?

英文:

How can i repeat statement "enter marks or grade" with in while loop.In this case every time control back to start of loop. and i want to repeat enter marks until user want to leave?


  1. public class Marks2 {
  2. public static void main(String args[]){
  3. Scanner s=new Scanner(System.in);
  4. System.out.println(&quot;Here is Subject list&quot;);
  5. System.out.println(&quot;1-Physics&quot;);
  6. System.out.println(&quot;2-Math&quot;);
  7. System.out.println(&quot;3-computer&quot;);
  8. boolean input=true;
  9. while (input){
  10. System.out.println(&quot;Enter subject number here&quot;);
  11. String sub=s.nextLine();
  12. if(sub.equals(&quot;1&quot;)) {
  13. System.out.println(&quot;Physics&quot;);
  14. System.out.println(&quot;&quot;);
  15. System.out.println(&quot;Enter marks /Enter change for change /Enter exit for leave&quot;);
  16. String change1 = null;
  17. String change2 = null;
  18. int marks = 0;
  19. try {
  20. change1 = s.nextLine();
  21. marks = Integer.parseInt(change1);
  22. } catch (Exception e) {
  23. System.out.println(&quot;Please enter only string value&quot;);
  24. change2 = s.nextLine();
  25. if (change2.equals(&quot;change&quot;)) {
  26. continue;
  27. } else if (change2.equals(&quot;exit&quot;)) {
  28. System.exit(0);
  29. }
  30. }
  31. if(marks&lt;40){
  32. System.out.println(&quot;Student is fail&quot;);
  33. }
  34. else if(marks==40){
  35. System.out.println(&quot;Student is fail he need more practice&quot;);
  36. }
  37. else if(marks&lt;70){
  38. System.out.println(&quot;need more practice but also good&quot;);
  39. }
  40. else if(marks==70){
  41. System.out.println(&quot;Good&quot;);
  42. }
  43. else if(marks&lt;90){
  44. System.out.println(&quot;Good but also Excellent&quot;);
  45. }
  46. else if(marks==90){
  47. System.out.println(&quot;Excellent&quot;);
  48. }
  49. else if(marks&lt;100){
  50. System.out.println(&quot;Outstanding&quot;);
  51. }
  52. else if(marks==100){
  53. System.out.println(&quot;Good but also excellent&quot;);
  54. }
  55. else if(change1.equals(&quot;change&quot;)){
  56. continue;
  57. }
  58. else if(change2.equals(&quot;exit&quot;)){
  59. System.exit(0);
  60. }
  61. else {
  62. System.out.println(&quot;&quot;);
  63. }
  64. continue;
  65. }
  66. }
  67. }
  68. }

After continue control went to start of loop and ask to enter subject again. Is it possible to enter grade until user wants to leave?

答案1

得分: 1

你需要在第一个循环内部再加入一个 while 循环,如果我理解正确的话,你想在进入 "change" 后更改主题,如果是这样,那么这段代码将会有效:

  1. public static void main(String args[])
  2. {
  3. Scanner s = new Scanner(System.in);
  4. System.out.println("科目列表如下:");
  5. System.out.println("1-物理");
  6. System.out.println("2-数学");
  7. System.out.println("3-计算机");
  8. setSubject:
  9. while (true)
  10. {
  11. System.out.println("请输入科目编号:");
  12. String sub = s.nextLine();
  13. if (sub.equals("1"))
  14. {
  15. System.out.println("物理");
  16. int marks = 0;
  17. while (true)
  18. {
  19. System.out.println("输入分数 / 输入 change 更改科目 / 输入 exit 退出");
  20. if (s.hasNextInt())
  21. {
  22. marks = s.nextInt();
  23. s.nextLine();
  24. } else
  25. {
  26. String command = s.nextLine();
  27. if (command.equalsIgnoreCase("exit")) break setSubject;
  28. else if (command.equalsIgnoreCase("change")) continue setSubject;
  29. else
  30. {
  31. System.out.println("请输入有效选项");
  32. continue;
  33. }
  34. }
  35. if (marks < 40)
  36. {
  37. System.out.println("学生不及格");
  38. } else if (marks == 40)
  39. {
  40. System.out.println("学生不及格,需要更多练习");
  41. } else if (marks < 70)
  42. {
  43. System.out.println("需要更多练习,但也不错");
  44. } else if (marks == 70)
  45. {
  46. System.out.println("良好");
  47. } else if (marks < 90)
  48. {
  49. System.out.println("不错,也很优秀");
  50. } else if (marks == 90)
  51. {
  52. System.out.println("优秀");
  53. } else if (marks < 100)
  54. {
  55. System.out.println("杰出");
  56. } else if (marks == 100)
  57. {
  58. System.out.println("不错,也很优秀");
  59. } else
  60. {
  61. System.out.println("");
  62. }
  63. }
  64. }
  65. }
  66. }

一个样本运行

  1. 科目列表如下:
  2. 1-物理
  3. 2-数学
  4. 3-计算机
  5. 请输入科目编号:
  6. 1
  7. 物理
  8. 输入分数 / 输入 change 更改科目 / 输入 exit 退出
  9. change
  10. 请输入科目编号:
  11. 1
  12. 物理
  13. 输入分数 / 输入 change 更改科目 / 输入 exit 退出
  14. 50
  15. 需要更多练习,但也不错
  16. 输入分数 / 输入 change 更改科目 / 输入 exit 退出
  17. 60
  18. 需要更多练习,但也不错
  19. 输入分数 / 输入 change 更改科目 / 输入 exit 退出
  20. exit
  21. 进程以退出代码 0 结束

使用一个 while 循环:

  1. public static void main(String args[])
  2. {
  3. Scanner s = new Scanner(System.in);
  4. System.out.println("科目列表如下:");
  5. System.out.println("1-物理");
  6. System.out.println("2-数学");
  7. System.out.println("3-计算机");
  8. String subject = "";
  9. int marks = 0;
  10. while (true)
  11. {
  12. if (subject.isEmpty())
  13. {
  14. System.out.println("请输入科目编号:");
  15. subject = s.nextLine();
  16. }
  17. if (subject.equals("1"))
  18. {
  19. System.out.println("物理");
  20. System.out.println("输入分数 / 输入 change 更改科目 / 输入 exit 退出");
  21. if (s.hasNextInt())
  22. {
  23. marks = s.nextInt();
  24. s.nextLine();
  25. } else
  26. {
  27. String command = s.nextLine();
  28. if (command.equalsIgnoreCase("exit")) break ;
  29. else if (command.equalsIgnoreCase("change")) {
  30. subject = "";
  31. continue ;
  32. }
  33. else
  34. {
  35. System.out.println("请输入有效选项");
  36. continue;
  37. }
  38. }
  39. if (marks < 40)
  40. {
  41. System.out.println("学生不及格");
  42. } else if (marks == 40)
  43. {
  44. System.out.println("学生不及格,需要更多练习");
  45. } else if (marks < 70)
  46. {
  47. System.out.println("需要更多练习,但也不错");
  48. } else if (marks == 70)
  49. {
  50. System.out.println("良好");
  51. } else if (marks < 90)
  52. {
  53. System.out.println("不错,也很优秀");
  54. } else if (marks == 90)
  55. {
  56. System.out.println("优秀");
  57. } else if (marks < 100)
  58. {
  59. System.out.println("杰出");
  60. } else if (marks == 100)
  61. {
  62. System.out.println("不错,也很优秀");
  63. } else
  64. {
  65. System.out.println("");
  66. }
  67. marks = 0;
  68. }
  69. }
  70. }
英文:

You need another while loop inside the first one, if i understand right, you want to change the subject once you enter change? if that is the case then this will work:

  1. public static void main(String args[])
  2. {
  3. Scanner s = new Scanner(System.in);
  4. System.out.println(&quot;Here is Subject list&quot;);
  5. System.out.println(&quot;1-Physics&quot;);
  6. System.out.println(&quot;2-Math&quot;);
  7. System.out.println(&quot;3-computer&quot;);
  8. setSubject:
  9. while (true)
  10. {
  11. System.out.println(&quot;Enter subject number here&quot;);
  12. String sub = s.nextLine();
  13. if (sub.equals(&quot;1&quot;))
  14. {
  15. System.out.println(&quot;Physics&quot;);
  16. int marks = 0;
  17. while (true)
  18. {
  19. System.out.println(&quot;Enter marks /Enter change for change /Enter exit for leave&quot;);
  20. if (s.hasNextInt())
  21. {
  22. marks = s.nextInt();
  23. s.nextLine();
  24. } else
  25. {
  26. String command = s.nextLine();
  27. if (command.equalsIgnoreCase(&quot;exit&quot;)) break setSubject;
  28. else if (command.equalsIgnoreCase(&quot;change&quot;)) continue setSubject;
  29. else
  30. {
  31. System.out.println(&quot;Please enter a valid option&quot;);
  32. continue;
  33. }
  34. }
  35. if (marks &lt; 40)
  36. {
  37. System.out.println(&quot;Student is fail&quot;);
  38. } else if (marks == 40)
  39. {
  40. System.out.println(&quot;Student is fail he need more practice&quot;);
  41. } else if (marks &lt; 70)
  42. {
  43. System.out.println(&quot;need more practice but also good&quot;);
  44. } else if (marks == 70)
  45. {
  46. System.out.println(&quot;Good&quot;);
  47. } else if (marks &lt; 90)
  48. {
  49. System.out.println(&quot;Good but also Excellent&quot;);
  50. } else if (marks == 90)
  51. {
  52. System.out.println(&quot;Excellent&quot;);
  53. } else if (marks &lt; 100)
  54. {
  55. System.out.println(&quot;Outstanding&quot;);
  56. } else if (marks == 100)
  57. {
  58. System.out.println(&quot;Good but also excellent&quot;);
  59. } else
  60. {
  61. System.out.println(&quot;&quot;);
  62. }
  63. }
  64. }
  65. }
  66. }

A Sample Run

  1. Here is Subject list
  2. 1-Physics
  3. 2-Math
  4. 3-computer
  5. Enter subject number here
  6. 1
  7. Physics
  8. Enter marks /Enter change for change /Enter exit for leave
  9. change
  10. Enter subject number here
  11. 1
  12. Physics
  13. Enter marks /Enter change for change /Enter exit for leave
  14. 50
  15. need more practice but also good
  16. Enter marks /Enter change for change /Enter exit for leave
  17. 60
  18. need more practice but also good
  19. Enter marks /Enter change for change /Enter exit for leave
  20. exit
  21. Process finished with exit code 0

With one while loop:

  1. public static void main(String args[])
  2. {
  3. Scanner s = new Scanner(System.in);
  4. System.out.println(&quot;Here is Subject list&quot;);
  5. System.out.println(&quot;1-Physics&quot;);
  6. System.out.println(&quot;2-Math&quot;);
  7. System.out.println(&quot;3-computer&quot;);
  8. String subject = &quot;&quot;;
  9. int marks = 0;
  10. while (true)
  11. {
  12. if (subject.isEmpty())
  13. {
  14. System.out.println(&quot;Enter subject number here&quot;);
  15. subject = s.nextLine();
  16. }
  17. if (subject.equals(&quot;1&quot;))
  18. {
  19. System.out.println(&quot;Physics&quot;);
  20. System.out.println(&quot;Enter marks /Enter change for change /Enter exit for leave&quot;);
  21. if (s.hasNextInt())
  22. {
  23. marks = s.nextInt();
  24. s.nextLine();
  25. } else
  26. {
  27. String command = s.nextLine();
  28. if (command.equalsIgnoreCase(&quot;exit&quot;)) break ;
  29. else if (command.equalsIgnoreCase(&quot;change&quot;)) {
  30. subject = &quot;&quot;;
  31. continue ;
  32. }
  33. else
  34. {
  35. System.out.println(&quot;Please enter a valid option&quot;);
  36. continue;
  37. }
  38. }
  39. if (marks &lt; 40)
  40. {
  41. System.out.println(&quot;Student is fail&quot;);
  42. } else if (marks == 40)
  43. {
  44. System.out.println(&quot;Student is fail he need more practice&quot;);
  45. } else if (marks &lt; 70)
  46. {
  47. System.out.println(&quot;need more practice but also good&quot;);
  48. } else if (marks == 70)
  49. {
  50. System.out.println(&quot;Good&quot;);
  51. } else if (marks &lt; 90)
  52. {
  53. System.out.println(&quot;Good but also Excellent&quot;);
  54. } else if (marks == 90)
  55. {
  56. System.out.println(&quot;Excellent&quot;);
  57. } else if (marks &lt; 100)
  58. {
  59. System.out.println(&quot;Outstanding&quot;);
  60. } else if (marks == 100)
  61. {
  62. System.out.println(&quot;Good but also excellent&quot;);
  63. } else
  64. {
  65. System.out.println(&quot;&quot;);
  66. }
  67. marks = 0;
  68. }
  69. }
  70. }

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

发表评论

匿名网友

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

确定