繁体   English   中英

用java调用/保存方法变量

[英]Calling/saving methods variables with java

我正在尝试使用 java 中的方法制作银行程序,它可以很好地通过程序一次,但是当它返回重复该过程时,它似乎没有保存以前完成的变量。 例如,我以 10 美元开始,然后存入 5 美元,它显示我有 15 美元,但是一旦我回去查看“帐户信息”,它就会重新启动变量的值,并恢复到其原始值 10 美元。 我怎样才能回去重复这个过程,同时每次都取改变的值。 希望这是有道理的...

package learning;
import java.util.Scanner;

public class Account {
    Scanner input = new Scanner(System.in);
    double balance;

    public double depos(double x) {
        System.out.println("how much do you want to deposit?");
        x+=input.nextDouble();
        balance=x;
        return balance;
    }
    public double withdraw(double x) {
        System.out.println("how much do you want to withdraw?");
        x-=input.nextDouble();
        balance = x;
        return balance;
    }
    public double info(double x) {
        balance = x;
        return balance;
    }

    public static void main(String[] args) {
        Account acc =  new Account();
        Scanner input = new Scanner(System.in);
        int choice = 0;
        double money;
        System.out.println("enter your initial balance!");
        money = input.nextDouble();

        System.out.println("Welcome!");

        while (choice !=4) {
            System.out.println("1.Deposit");
            System.out.println("2.Withdraw");
            System.out.println("3.Check Account Info");
            System.out.println("4.Exit");
            choice = input.nextInt();

            switch (choice) {
            case 1:
                double pick1 = acc.depos(money);
                System.out.println("you now have Rp " +pick1+"\n");
                break;  
            case 2:
                double pick2 = acc.withdraw(money);
                System.out.println("You now have RP "+pick2+"\n");
                break;
            case 3:
                double pick3 = acc.info(money);
                System.out.println("your current balance is Rp "+pick3+"\n");
                break;
            case 4:
                System.out.println("thank you!");
                break;
            }
        }
    }
}

您不应该要求Account类中的值,而应该只在 main 中要求并使用构造函数作为初始值

public class Account {
    double balance;

    public Account(double init){
        balance = init;
    }

    public double depos(double val) {
        balance += val;
        return balance;
    }

    public double withdraw(double val) {
        balance -= val;
        return balance;
    }

    public double info() {
        return balance;
    }
}

而主要的

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int choice = 0;
    System.out.println("enter your initial balance!");
    double money = input.nextDouble();
    Account acc =  new Account(money);

    while (choice !=4) {
        System.out.println("1.Deposit\n2.Withdraw\n3.Check Account Info\n4.Exit");
        choice = input.nextInt();

        switch (choice) {
        case 1:
            System.out.println("how much do you want to deposit?");
            double pick1 = acc.depos(input.nextDouble());
            System.out.println("you now have Rp " +pick1+"\n");
            break;  
        case 2:
            System.out.println("how much do you want to withdraw?");
            double pick2 = acc.withdraw(input.nextDouble());
            System.out.println("You now have RP "+pick2+"\n");
            break;
        case 3:
            double pick3 = acc.info();
            System.out.println("your current balance is Rp "+pick3+"\n");
            break;
        case 4:
            System.out.println("thank you!");
            break;
        }
    }
}

那是因为acc ooject 中的余额没有用最新值更新:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM