Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用While循环Java银行帐户_Java - Fatal编程技术网

使用While循环Java银行帐户

使用While循环Java银行帐户,java,Java,我应该在哪里添加while循环?我试了又试,结果犯了太多错误,我不知道如何循环。我需要它循环显示账户金额的部分,然后存款和取款,这样你就可以一直存款和取款,直到你想退出为止,如果你输入-1,就需要它退出。通常,当程序员需要在计算机上“膨胀”一个想法时,如果这个想法很复杂(程序员明智)他们真的得先在纸上画出来 您正在处理一个算法,这自然意味着您需要首先设计它,这样它在实现时就可以很好地工作 TL;DR 如果我们慢慢地写出应该正确处理此代码段的伪代码,它将如下所示: import java.util

我应该在哪里添加while循环?我试了又试,结果犯了太多错误,我不知道如何循环。我需要它循环显示账户金额的部分,然后存款和取款,这样你就可以一直存款和取款,直到你想退出为止,如果你输入-1,就需要它退出。

通常,当程序员需要在计算机上“膨胀”一个想法时,如果这个想法很复杂(程序员明智)他们真的得先在纸上画出来

您正在处理一个算法,这自然意味着您需要首先设计它,这样它在实现时就可以很好地工作

TL;DR 如果我们慢慢地写出应该正确处理此代码段的伪代码,它将如下所示:

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());
}
}

像这样吗?你好像是在没有一个真正计划的情况下开始编写的。我建议退一步,拿一支铅笔和一些纸,在写任何东西之前写下程序的结构和你希望它执行的操作。我在课堂上收到了这段代码,并告诉我修改它以添加draw和loop。我添加了draw,但是当我画出循环时,它在代码中没有平移。
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