0

这是代码:

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 循环。

我看过牙套,认为这不是问题。

4

3 回答 3

1

你是说Y还是y

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

您的代码正在测试同一件事,即y两次。


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

while (keepGoing.equalsIgnoreCase("y")) 
于 2012-12-17T05:50:20.980 回答
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
于 2012-12-17T05:58:30.763 回答
0

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

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

于 2012-12-17T07:07:18.093 回答