簡體   English   中英

嵌套的while循環不循環嗎?

[英]Nested while loop not looping?

這是代碼:

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

我看過牙套,但不認為這是問題所在。

您是說Y還是y

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

您的代碼對同一件事(即y )進行了兩次測試。


僅供參考,您的測試可以簡化為:

while (keepGoing.equalsIgnoreCase("y")) 

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

如果我可以靈活地更改框架程序,則可以使用布爾變量。

但是,您必須采用的解決方案是將keepGoing重置為“ y”,因為將其設置為“ n”會導致其被跳過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM