需要通过方法保留检查和储蓄变量的值

时间:2015-04-09 23:06:45

标签: java

我很难保持检查和储蓄的价值,并且能够显示检查和节省电流平衡。

package atmmethod;

import java.util.Locale;
import java.util.Scanner;

/**
 *
 * @author jfumar
 */
public class Atmmethod {

private static int option;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    //prompts user with options in simulation using methods

    do {
        System.out.println("Welcome to the new and improved atm program with methods!");
        displayMainMenu();
        double savings = 1000;
        double checkings = 500;
        System.out.println("savings balance is: " + savings);
        System.out.println("checking balance is:" + checkings);

        Scanner input = new Scanner(System.in);
        System.out.print("Enter option here: ");
        int option = input.nextInt();

        switch (option) {
            case 1:
                depositOption();
                break;
            case 2:
                withdrawOption();
                break;
            case 3:
                checkingBalance();
                break;
            default:
                System.out.print("Goodbye!");
                System.exit(0);

        }

    } while (option != 4);
    System.out.println("thank you for using the new atm");

}

public static void displayMainMenu() {
    System.out.println("Select options by numbers 1, 2, 3, or 4");
    System.out.println("1. deposit");
    System.out.println("2. withdraw");
    System.out.println("3. balance");
    System.out.println("4. exit");

}

public static void depositOption() {
    double currentSavings = 1000;
    double currentCheckings = 500;
    double amount;

    System.out.println("where would like to deposit your money?");
    System.out.println("1. Savings");
    System.out.println("2. Checkings");
    Scanner input = new Scanner(System.in);
    System.out.print("Enter choice here: ");
    int choice = input.nextInt();

    if (choice == 1) {
        System.out.println("how much do you want to deposit in your savings?");
        amount = input.nextDouble();
        currentSavings += amount;
        System.out.println("your savings balance is now: " + currentSavings);
    } else {
        System.out.println("how much do you want to deposit in your checkings?");
        amount = input.nextDouble();
        currentCheckings += amount;
        System.out.println("your checkings balance is now: " + currentCheckings);

    }

}

public static void withdrawOption() {
    double currentSavings = 1000;
    double currentCheckings = 500;
    double amount;

    System.out.println("where would like to withdraw your money?");
    System.out.println("1. Savings");
    System.out.println("2. Checkings");
    Scanner input = new Scanner(System.in);
    System.out.print("Enter choice here: ");
    int choice = input.nextInt();

    if (choice == 1) {
        System.out.println("how much do you want to withdraw from your savings?");
        amount = input.nextDouble();
        currentSavings -= amount;
        System.out.println("your savings balance is now: " + currentSavings);
    } else {
        System.out.println("how much do you want to withdraw from your checkings?");
        amount = input.nextDouble();
        currentCheckings -= amount;
        System.out.println("your checkings balance is now: " + currentCheckings);

    }

}

public static void checkingBalance() {
    System.out.println("CURRENT BALANCES");
    System.out.println("Savings balance is: ");
    System.out.println("Checkings balance is: ");
}

}

2 个答案:

答案 0 :(得分:0)

储蓄和检查应该是您班级的字段,而不是本地变量。在方法结束时销毁局部变量。

public class Atmmethod {

    private static double currentSavings = 500;
    private static double currentCheckings = 1000;

    //...

}

顺便说一下,你所有的方法和领域都是静态的,这不是好事,而是另一个故事

答案 1 :(得分:0)

我认为你想要的是检查和储蓄的全局变量,除非我误解了你的问题。每当运行depositOption()和withdrawOption()时,它会将余额重置为1000和500.只需将currentSavings和currentCheckings设为全局变量,然后在提取和存款中,添加/减去用户从全局变量输入的数量。