嵌套while循环不循环?

时间:2012-12-17 05:45:22

标签: java

以下是代码:

while (keepGoingDay.equals("y") || keepGoingDay.equals("y")){
        System.out.println(acct1);
        System.out.println(acct2);
        Account.reset();

        while (keepGoing.equals("y") || keepGoing.equals("y"))
            {
            //get account number, what to do, and amount
            System.out.print("\nEnter the number of the account you would like to access: ");
            acctNumber = scan.nextLong();
            System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
            action = scan.next();
            System.out.print("Enter the amount: ");
            amount = scan.nextDouble();

            if (amount > 0)
                if (acctNumber == acct1.getAcctNumber())
                if (action.equals("w") || action.equals("W"))
                    acct1.withdraw(amount);
                else if (action.equals("d") || action.equals("D"))
                    acct1.deposit(amount);
                else 
                    System.out.println("Sorry, invalid action.");
                else if (acctNumber == acct2.getAcctNumber())
                if (action.equals("w") || action.equals("W"))
                    acct1.withdraw(amount);
                else if (action.equals("d") || action.equals("D"))
                    acct1.deposit(amount);
                else 
                    System.out.println("Sorry, invalid action.");
                else
                System.out.println("Sorry, invalid account number.");
            else
                System.out.println("Sorry, amount must be > 0.");


            System.out.print("\nMore transactions? (y/n)");
            keepGoing = scan.next();        
            }
        System.out.println("End of day stats: ");
        System.out.println("Number of deposits: " + Account.getNumDeposits());
        System.out.println("Number of withdrawals: " + Account.getNumWithdrawals());
        System.out.println("Total value of deposits: " + Account.getTotalDeposits());
        System.out.println("Total value of withdrawals: " + Account.getTotalWithdrawals());
        System.out.print("More days?");
        keepGoingDay = scan.next();
         }

}

我不认为这些方法对此非常重要,所以我会将它们留出来以节省空间。

此计划的目标是记录和计算多天的交易(未知金额,因此我无法使用for循环)。

它经历了第一次运行,之后,它跳过内部while循环。

我看过大括号,不认为这是问题所在。

3 个答案:

答案 0 :(得分:1)

您的意思是Y还是y

while (keepGoing.equals("Y") || keepGoing.equals("y")) 

您的代码正在测试相同的内容,即y,两次。


仅供参考,您的测试可以简化为:

while (keepGoing.equalsIgnoreCase("y")) 

答案 1 :(得分:0)

scan.next();放在acctNumber = scan.nextLong();行和amount = scan.nextDouble();行之后。

喜欢这个。检查我新添加的行。

            System.out.print("\nEnter the number of the account you would like to access: ");
            acctNumber = scan.nextLong();
            scan.next(); // newly added
            System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
            action = scan.next();
            System.out.print("Enter the amount: ");
            amount = scan.nextDouble();
            scan.next(); // newly added

答案 2 :(得分:0)

如果我可以灵活地更改骨架程序,我会使用布尔变量。

但是,您必须设置的解决方案是将keepGoing重置为“y”,因为它被设置为“n”,导致它被跳过。