如果循环内的分支未按预期执行

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

if branches within while loop not executing as expected

问题

这是我正在使用的代码。这里引用的其他类已经经过测试并且有效,尽管它们不应该影响这里的语句的功能。

  1. import java.util.Scanner;
  2. public class TransferTest
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner scan = new Scanner(System.in);
  7. double transferAmount;
  8. Account acct1 = new Account(100, null);
  9. Account acct2 = new Account(100, null);
  10. System.out.print("选择一个选项:"
  11. + "\n1 <Enter>: 从帐户1转帐到帐户2"
  12. + "\n2 <Enter>: 从帐户2转帐到帐户1"
  13. + "\n3 <Enter>: 退出");
  14. int userOption = scan.nextInt();
  15. scan.nextLine();
  16. while(userOption != 3);
  17. {
  18. if (userOption == 1)
  19. {
  20. System.out.print("您选择了从帐户1转帐到帐户2"
  21. + "\n输入要转帐的金额:");
  22. transferAmount = scan.nextInt();
  23. acct1.transfer(acct2, transferAmount);
  24. }
  25. else if (userOption == 2)
  26. {
  27. System.out.print("您选择了从帐户2转帐到帐户1"
  28. + "\n输入要转帐的金额:");
  29. transferAmount = scan.nextInt();
  30. acct2.transfer(acct1, transferAmount);
  31. }
  32. else if (userOption == 3)
  33. {
  34. System.out.println(acct1.toString());
  35. System.out.println(acct2.toString());
  36. }
  37. else
  38. {
  39. System.out.println("无效的输入");
  40. }
  41. System.out.print("选择一个选项:"
  42. + "\n1 <Enter> 从帐户1转帐到帐户2?"
  43. + "\n2 <Enter> 从帐户2转帐到帐户1"
  44. + "\n3 <Enter> 退出");
  45. userOption = scan.nextInt();
  46. scan.nextLine();
  47. }
  48. System.out.println(acct1.toString());
  49. System.out.println(acct2.toString());
  50. scan.close();
  51. }
  52. }

我想要它做的是提示用户选择由数字值表示的三个选项之一。然后根据该值的条件分支,如果是1或2,则要求用户输入金额。只有当用户选择选项3时才退出while循环。发生的情况是,当输入3或指定范围之外的值时,程序按预期工作,但当选择1或2作为选项时,什么都不会发生,甚至不会输出打印语句。非常感谢您帮助我解决这个问题。

英文:

Here is the code that I am working with. The other classes that are referenced here have been tested and work though they should not have an effect on the functionality of the statements here.

  1. import java.util.Scanner;
  2. public class TransferTest
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner scan = new Scanner(System.in);
  7. double transferAmount;
  8. Account acct1 = new Account(100, null);
  9. Account acct2 = new Account(100, null);
  10. System.out.print(&quot;Choose an option: &quot;
  11. + &quot;\n1 &lt;Enter&gt;: to transfer from account 1 to account 2&quot;
  12. + &quot;\n2 &lt;Enter&gt;: to transfer from account 2 to account 1&quot;
  13. + &quot;\n3 &lt;Enter&gt;: to quit&quot;);
  14. int userOption = scan.nextInt();
  15. scan.nextLine();
  16. while(userOption != 3);
  17. {
  18. if (userOption == 1)
  19. {
  20. System.out.print(&quot;You chose transfer from account 1 to account 2&quot;
  21. + &quot;\nEnter an amount to transfer: &quot;);
  22. transferAmount = scan.nextInt();
  23. acct1.transfer(acct2, transferAmount);
  24. }
  25. else if (userOption == 2)
  26. {
  27. System.out.print(&quot;You chose transfer from account 1 to account 2&quot;
  28. + &quot;\nEnter an amount to transfer: &quot;);
  29. transferAmount = scan.nextInt();
  30. acct2.transfer(acct1, transferAmount);
  31. }
  32. else if (userOption == 3)
  33. {
  34. System.out.println(acct1.toString());
  35. System.out.println(acct2.toString());
  36. }
  37. else
  38. {
  39. System.out.println(&quot;Invalid input&quot;);
  40. }
  41. System.out.print(&quot;Choose an option: &quot;
  42. + &quot;\n1 &lt;Enter&gt; to transfer from account 1 to account 2?&quot;
  43. + &quot;\n2 &lt;Enter&gt; to transfer from account 2 to account 1&quot;
  44. + &quot;\n3 &lt;Enter&gt; to quit&quot;);
  45. userOption = scan.nextInt();
  46. scan.nextLine();
  47. }
  48. System.out.println(acct1.toString());
  49. System.out.println(acct2.toString());
  50. scan.close();
  51. }
  52. }

What I'm wanting this to do is prompt a user to choose one of the three options denoted by a numerical value. Then follow the if branch of that value, asking the user for an amount in the case of 1 or 2. Not exiting the while loop until the user chooses option 3. Whats happening is that when 3 or a value outside the specified range is entered the program works as expected, but when 1 or 2 are chosen as options nothing happens, not even the print statements are output. Any help with getting this working is much appreciated.

答案1

得分: 5

你的 while 循环后面有一个分号。

  1. while(userOption != 3);

等效于:

  1. while(userOption != 3) {
  2. // 什么都不做
  3. }

因此,在该行之后的代码都不会被执行。

英文:

You have a semi-colon after your while loop.

  1. while(userOption != 3);

Is effectively equal to:

  1. while(userOption != 3) {
  2. // do nothing
  3. }

So no code after that line is being executed.

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

发表评论

匿名网友

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

确定