Java - 运行总计(使用do while和if else语句)

时间:2014-10-27 16:19:18

标签: java if-statement do-while

我不是很擅长使用Java来获取正在运行的总数,因为我最近刚开始使用Java。我必须显示并保持银行余额的运行总额,并且由于某些奇怪的原因,它会重新设置为100,这就是我宣布的开始。有没有办法阻止银行余额在每次循环时重置?

public static void main(String args[])
{
    Scanner sc = new Scanner(System.in);
    int choice, totBal = 0, totWith = 0, totDep = 0;
    double with, remBal = 0, deposit, bankBal = 100;
    char reply = 0;

    do
    {
        System.out.println("");
        System.out.println("Bank online\n");
        System.out.println("1. Withdraw");
        System.out.println("2. Deposit");
        System.out.println("3. Balance");
        System.out.println("4. Account Details");
        System.out.println("5. Exit\n");
        System.out.print("Enter your choice: ");
        choice = sc.nextInt();

        if(choice == 1)
        {
            System.out.print("How much do you wish to withdraw?\n");
            with = sc.nextInt();
            remBal = bankBal - with;
            System.out.println("Your new balance is: " + remBal);
            totWith++;
        }
        else if(choice == 2)
        {  
            System.out.print("How much do you wish to deposit?\n");
            deposit = sc.nextInt();
            remBal = remBal + deposit;
            System.out.println("Your new balance is: " + remBal);
            totDep++;
        }
        else if(choice == 3)
        {
            System.out.println("Your balance is: " + remBal);
            totBal++;
        }
        else if(choice == 4)
        {
            System.out.println("You made " + totWith + " withdrawls from your account.");
            System.out.println("You made " + totDep + " deposits to your account.");
            System.out.println("You made " + totBal + " balance checks on your account.");
        }
        else if(choice == 5)
        {

        }

        System.out.println("");
        System.out.print("Do you want to enter another option?(y/n): ");
        reply = sc.next().charAt(0);

    }while(reply == 'Y' || reply == 'y');

    System.out.println("Thank you and goodbye!");
}

}

另外,我觉得我有太多的变数。我怎样才能减少这些?

3 个答案:

答案 0 :(得分:0)

您的问题在于以下声明:

 double with, remBal = 0, deposit, bankBal = 100;

在这里,您将remBal初始化为0,而当您存入一笔金额/支票余额时:

 remBal = remBal + deposit;//you use remBal for blaance check

因此,在第一次尝试时,它将尝试添加0,说100美元,这将是100,而bankBal是100,它应该是100.因此初始化remBal与bankBal相同(或者只使用一个变量用于bankBalance,即任何一个)。 / p>

答案 1 :(得分:0)

在程序开始时将bankBal值设置为100。 在提款时,你总是这样做

remBal = bankBal - with

总是等于

remBal = 100 - with

因为您永远不会更改bankBal以反映每个循环后的更新余额。 解决此问题的一种方法是删除

bankBal

完全变量,只需设置

即可
remBal

变量到您想要的起始值。 最后将上述提款计算更改为

remBal = remBal - with

答案 2 :(得分:0)

你可以做的一件事是实现switch-cases来调用特定于Depost,Withdraw等的方法。这个例子大致是:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Please Enter what you would like to do: ");
    String enterPrompt = input.next();
    int options = 5;
    switch (options) {
        case 1:  enterPrompt = "Deposit";
                 doDeposit();
                 break;
        case 2:  enterPrompt = "Withdrawel";
                 doWithdrawel();
                 break;
        case 3:  enterPrompt = "Balance";
                 viewBalance();
                 break;
        case 4:  enterPrompt = "Account Details";
                 viewAccount();
                 break;
        case 5:  enterPrompt = "Exit";
                 System.exit(1);
                 break;
    }
     public void doDeposit(){
     //local variables here
     //Do stuff
      }
     public void doWithdrawel(){
     //local variables here
     //Do stuff
    }
     public void viewBalance(){
    //local variables here
    //Do stuff
    }
     public void viewAccount(){
    //local variables here
    //Do stuff
    }

}