Java 银行账户计划

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19275389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:37:49  来源:igfitidea点击:

Bank Account Program

java

提问by Morgan

I am working on making a program to simulate bank transactions. I have to ask the user if they want to deposit, withdrawal, or transfer.

我正在制作一个程序来模拟银行交易。我必须询问用户是否要存款、取款或转账。

When I deposit a certain amount (for example 1000), it says my balance is 1000. Then I ask to withdrawal a number like 400 it says my balance is -400. After all that I thought maybe I have to check my balance and then it will give me the correct balance of what should be 600, but it says 0. For instance, see this transcript:

当我存入一定金额(例如 1000)时,它说我的余额是 1000。然后我要求提款 400 之类的数字,它说我的余额是 -400。毕竟我想也许我必须检查我的余额,然后它会给我应该是 600 的正确余额,但它显示 0。例如,请参阅此成绩单:

screen capture of output

输出的屏幕截图

I was thinking because in my code (shown below) I made the balance = 0 but if I take the = 0 away and try to run the program it says it needs to be initialized.

我在想,因为在我的代码(如下所示)中,我设置了 balance = 0 但如果我把 = 0 拿走并尝试运行程序,它会说它需要初始化。

I am stuck and I want to figure it out. Please don't post the entire code corrected. I want to fix it myself and learn!

我被卡住了,我想弄清楚。请不要发布更正的整个代码。我想自己修好学习!

import java.util.Scanner;

public class BankTransactions {


    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int num;

        do {

            double balance = 0;
            double amount;

            System.out.println("Type Number");
            System.out.println("1. Deposit");
            System.out.println("2. Withdrawal");
            System.out.println("3. Balance");
            System.out.println("4. Exit");
            num = scan.nextInt();

            if (num == 1) {
                System.out.println("Enter amount to deposit: ");
                amount = scan.nextDouble();

                // Add the amount to the balance
                balance += amount;
                System.out.println("Your balance is");
                System.out.println(balance);


            } else if (num == 2) {

                System.out.println("Enter amount to withdrawal: ");
                amount = scan.nextDouble();

                // Remove the amount from the balance
                balance -= amount;
                System.out.println("Your balance is");
                System.out.println(balance);

            } else if (num == 3) {


                System.out.println("Your Balance");
                System.out.println(balance);

            }



        } while (num != 4);

        System.out.println("Good Bye!");

    }
}

采纳答案by luanjot

Every time the do{...} while{...} is executed, you are setting balance=0. You should take it out the loop.

每次执行 do{...} while{...} 时,您都设置 balance=0。你应该把它从循环中取出来。

double balance = 0;

do{
...

回答by Tom Swifty

You are initializing balance to 0 within the do loop, so it resets to zero each time around.

您在 do 循环中将 balance 初始化为 0,因此每次都会重置为零。

Move the line balance = 0 to above the while loop.

将行 balance = 0 移动到 while 循环上方。

回答by Bucket

Every time you run the loop you set balanceto 0. Move this to outside your doloop:

每次运行循环时都设置balance0. 将其移至do循环之外:

double balance = 0;
double amount;
do {
    /* code */
} while(num != 4);