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

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

if branches within while loop not executing as expected

问题

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

import java.util.Scanner;

public class TransferTest 
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);

        double transferAmount;
        Account acct1 = new Account(100, null);
        Account acct2 = new Account(100, null);

        System.out.print("选择一个选项:"
                + "\n1 <Enter>: 从帐户1转帐到帐户2"
                + "\n2 <Enter>: 从帐户2转帐到帐户1"
                + "\n3 <Enter>: 退出");
        int userOption = scan.nextInt();
        scan.nextLine();

        while(userOption != 3);  
        {
            if (userOption == 1)
            {
                System.out.print("您选择了从帐户1转帐到帐户2"
                        + "\n输入要转帐的金额:");
                transferAmount = scan.nextInt();
                acct1.transfer(acct2, transferAmount);
            }
            else if (userOption == 2)
            {
                System.out.print("您选择了从帐户2转帐到帐户1"
                        + "\n输入要转帐的金额:");
                transferAmount = scan.nextInt();
                acct2.transfer(acct1, transferAmount);
            }
            else if (userOption == 3)
            {
                System.out.println(acct1.toString());
                System.out.println(acct2.toString());
            }
            else
            {
                System.out.println("无效的输入");
            }

            System.out.print("选择一个选项:"
                    + "\n1 <Enter> 从帐户1转帐到帐户2?"
                    + "\n2 <Enter> 从帐户2转帐到帐户1"
                    + "\n3 <Enter> 退出");
            userOption = scan.nextInt();
            scan.nextLine();
        }

        System.out.println(acct1.toString());
        System.out.println(acct2.toString());
        scan.close();
    }
}

我想要它做的是提示用户选择由数字值表示的三个选项之一。然后根据该值的条件分支,如果是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.

import java.util.Scanner;
public class TransferTest 
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double transferAmount;
Account acct1 = new Account(100, null);
Account acct2 = new Account(100, null);
System.out.print(&quot;Choose an option: &quot;
+ &quot;\n1 &lt;Enter&gt;: to transfer from account 1 to account 2&quot;
+ &quot;\n2 &lt;Enter&gt;: to transfer from account 2 to account 1&quot;
+ &quot;\n3 &lt;Enter&gt;: to quit&quot;);
int userOption = scan.nextInt();
scan.nextLine();
while(userOption != 3);	
{
if (userOption == 1)
{
System.out.print(&quot;You chose transfer from account 1 to account 2&quot;
+ &quot;\nEnter an amount to transfer: &quot;);
transferAmount = scan.nextInt();
acct1.transfer(acct2, transferAmount);
}
else if (userOption == 2)
{
System.out.print(&quot;You chose transfer from account 1 to account 2&quot;
+ &quot;\nEnter an amount to transfer: &quot;);
transferAmount = scan.nextInt();
acct2.transfer(acct1, transferAmount);
}
else if (userOption == 3)
{
System.out.println(acct1.toString());
System.out.println(acct2.toString());
}
else
{
System.out.println(&quot;Invalid input&quot;);
}
System.out.print(&quot;Choose an option: &quot;
+ &quot;\n1 &lt;Enter&gt; to transfer from account 1 to account 2?&quot;
+ &quot;\n2 &lt;Enter&gt; to transfer from account 2 to account 1&quot;
+ &quot;\n3 &lt;Enter&gt; to quit&quot;);
userOption = scan.nextInt();
scan.nextLine();
}
System.out.println(acct1.toString());
System.out.println(acct2.toString());
scan.close();
}
}

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 循环后面有一个分号。

while(userOption != 3);

等效于:

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

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

英文:

You have a semi-colon after your while loop.

while(userOption != 3);

Is effectively equal to:

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

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:

确定