日期:2014-05-20  浏览次数:20816 次

新手请教大神,用带参数方法实现银行账户问题。

public class Account {




//带参存款


public double shape(double a ){




return a;
}

//带参取款

public double qukuan(double b){



return b;
}
}



package com.ban;

import java.util.Scanner;

public class test {


public static void main(String[] args) {
// TODO Auto-generated method stub



double a1 = 0;
double b1;
int count;
Scanner input = new Scanner(System.in);

Account a = new Account();

do {
System.out.println("1.存款 2.取款 0.退出");
System.out.println("请选择你需要办理的业务:");
count = input.nextInt();
switch (count) {
case 0:
System.out.println("谢谢使用");

break;
case 1:

System.out.println("请输入存款金额:");
a1 = input.nextDouble();

System.out.println("存款成功");


System.out.println("*****当前余额为:"+a.shape(a1)+"元*****");
a.shape(a.shape(a1));


break;
case 2:
System.out.println("请输入取款金额:");
b1 = input.nextDouble();

if (a.shape(a1)>=a.qukuan(b1)) {
System.out.println("取款成功");
System.out.println("*****当前余额为:"+(a.shape(a1)-a.qukuan(b1))+"元*****");
a.shape(a.shape(a1)-a.qukuan(b1));
}else{
System.out.println("错误!");
}

break;

default:
System.out.println("您的输入有误,请重新输入:");
count=input.nextInt();
break;
}
} while (count!=0);

}

}


第一次存的钱进去没有问题 ,,循环一次就把存的钱回零了。
我想让存取叠加。


------解决方案--------------------
在你的基础上稍微修正了一下。。。
Java code

import java.util.Scanner;

public class Testt {

    public static void main(String[] args) {

        double a1 = 0;
        double b1;
        int count;
        Scanner input = new Scanner(System.in);
        Account a = new Account();

        do {
            System.out.println("1.存款 2.取款 0.退出");
            System.out.println("请选择你需要办理的业务:");
            count = input.nextInt();
            switch (count) {
            case 0:
                System.out.println("谢谢使用");
                break;
            case 1:

                System.out.println("请输入存款金额:");
                a1 = input.nextDouble();
                System.out.println("存款成功");
                a.setSum(a.shape(a1));
                System.out.println("*****当前余额为:" + a.getSum() + "元*****");
                break;
            case 2:
                System.out.println("请输入取款金额:");
                b1 = input.nextDouble();
                if (a.getSum() >= a.qukuan(b1)) {
                    System.out.println("取款成功");
                    a.setSum(a.qukuan(b1));
                    System.out.println("*****当前余额为:" + a.getSum() + "元*****");
                } else {
                    System.out.println("错误!");
                }
                break;
            default:
                System.out.println("您的输入有误,请重新输入:");
                count = input.nextInt();
                break;
            }
        } while (count != 0);
    }
}

class Account {
    double sum = 0;

    // 带参存款
    public double shape(double a) {
        return sum + a;
    }

    // 带参取款
    public double qukuan(double b) {
        return sum - b;
    }

    public double getSum() {
        return sum;
    }

    public void setSum(double sum) {
        this.sum = sum;
    }
}