使用While循环Java银行帐户

时间:2018-10-02 18:15:32

标签: java

import java.util.Scanner;

public class AccountTest {
    public static void main(String[] args) {



        Account account1 = new Account("John Blue", 50.00);
        Account account2 = new Account ("Jane Green", -7.53);
        System.out.printf("To exit, enter -1 for deposit amount.");




        System.out.printf("%s balance: $%.2f%n",
            account1.getName(), account1.getBalance());
        System.out.printf("%s balance: $%.2f%n%n",
            account2.getName(), account2.getBalance());


        Scanner input = new Scanner(System.in);

        System.out.print("Enter deposit amount for account1: ");
        double depositAmount = input.nextDouble();
        System.out.printf("%nadding %.2f to account1 balance%n%n",
            depositAmount);
        account1.deposit(depositAmount);

        System.out.print("Enter withdraw amount for account1: ");
        double withdrawalAmount = input.nextDouble();
        System.out.printf("\nsubtracting %.2f from accojaunt1 balance\n",
            withdrawalAmount);
        account1.Withdraw(withdrawalAmount);


        System.out.printf("%s balance: $%.2f%n",
            account1.getName(), account1.getBalance());
        System.out.printf("%s balance: $%.2f%n%n",
            account2.getName(), account2.getBalance());

        System.out.print("Enter deposit amount for account2: ");
        depositAmount = input.nextDouble();
        System.out.printf("%nadding %.2f to account2 balance%n%n",
            depositAmount);
        account2.deposit(depositAmount);


        System.out.print("Enter withdrawal amount for account2: ");
        withdrawalAmount = input.nextDouble();
        System.out.printf("\nsubtracting %.2f from account2 balance\n",
            withdrawalAmount);
        account2.Withdraw(withdrawalAmount);

        System.out.printf("%s balance: $%.2f%n",
            account1.getName(), account1.getBalance());
        System.out.printf("%s balance: $%.2f%n%n",
            account2.getName(), account2.getBalance());
}
}

我应该在哪里添加while循环?我已经尝试了很多次,但又遇到了很多错误,我不知道该如何循环。我需要它在显示帐户金额然后存入和提取的部分上进行循环,以便您可以继续存入,提取和显示直到想要退出为止,如果输入-1,则需要退出。

1 个答案:

答案 0 :(得分:1)

通常,当程序员需要在计算机上对'inflate'一种想法进行排序时,如果这种想法很复杂(从程序员的角度来说),他们实际上必须首先在纸上画草图。

您正在处理一种算法,这自然表明您需要首先设计它,因此在实施时效果很好。

TL; DR

如果我们慢慢写出应能正确运行此代码段的伪代码,则看起来像这样:

start

while true do the following
    ask for some input
    if some input was -1, break out of loop
    else do some fancy calculations

end