我有一个小额存款,提款,查看余额和退出的账户。一切运行正常,但是我希望根据我使用的方法(提款,入金等)来调整余额,我从joptionpane开始,但遇到了同样的问题,并认为我应该重新开始。有人指出我正确的方向吗?

主文件:

import java.util.Scanner;

public class Bank1
{
    public static void main(String[] args)
    {
        HomeBank obj1 = new HomeBank();

        Scanner keyboard = new Scanner(System.in);
        String option;
        char object;

        System.out.println("This is your home banking account" + "\n");

        do
        {
            System.out.println("What would you like to do today?"+ "\n" +
                    "\nSelect the following: " +
                    "\n'B' To View Balance." +
                    "\n'D' To Deposit" +
                    "\n'W' To Withdrawal" +
                    "\n'E' To Exit");

            System.out.print("Your selection: ");

            option = keyboard.next().toUpperCase();
            object = option.charAt(0);

            System.out.print("\n");
            System.out.println("you entered: " +object+ "\n");

            if(object =='D')
                obj1.deposit();
            else if(object =='W')
                obj1.withdrawal();
            else if(object =='B')
                obj1.balance();
            else
                System.out.println("**Invalid input**" +
                        "\n Please try again");
        } while(object !='E');

        System.out.println("The End");
    }
}


类:

import java.util.InputMismatchException;
import java.util.Scanner;

public class HomeBank
{
    private double withdrawal, deposit, balance;

    public HomeBank()
    {
        balance = 100;
        withdrawal = 50;
        deposit = 150;
    }

    public HomeBank(double bal, double with, double de)
    {
        balance = bal;
        withdrawal = with;
        deposit = de;
    }

    public static void balance()
    {
        double balance = 250.00d;

        System.out.println("You have $" + balance+"dollars");
    }

    public static void deposit()
    {
        Scanner keyboard = new Scanner(System.in);
        double deposit = 0.00d;
        double balance = 250.00d;
        boolean goodput =true;

        do
        {
            try
            {
                System.out.print("How much would you like to deposit today? :");
                deposit = keyboard.nextDouble();

                if(deposit > 0.00)
                {
                    System.out.println("you entered $" +deposit+" dollars");
                    System.out.println("you now have $" + (deposit + balance)+" dollars");
                    goodput = false;
                }
                else
                {
                    System.out.println("\n**Error**\nYou cannot deposit a "
                            + "negative amount\n");
                    System.out.println("Please try again\n");
                    goodput = true;
                }
            }
            catch (InputMismatchException x)
            {
                System.out.println("I'm sorry but that is an invalid input" +
                        "\nYou will be redirected to the main menu shortly..."+
                        "\n");
                goodput = false;
            }
        } while (goodput == true);
    }

    public static void withdrawal()
    {
        Scanner keyboard = new Scanner(System.in);
        double withdrawal = 0.00d;
        double balance = 250.00d;
        boolean goodput = true;

        do
        {
            try
            {
                System.out.println("How much would you like to withdrawal?");
                withdrawal = keyboard.nextDouble();

                if (withdrawal < 0 || withdrawal > balance)
                {
                    System.out.println("You have either entered a negative number"
                            + "or trying to withdrawal more than is in your account");
                    System.out.println("You cannot withdrawal more than $"+balance);
                    System.out.println("Please try again");
                    goodput = true;
                }
                else
                {
                    System.out.println("You now have $" +(balance-withdrawal)+ "dollars");
                    System.out.println("Thank you for choosing HomeBank\n"
                            + "\nYou will be redirected to the main menu\n");
                    goodput =false;
                }
            }
            catch (InputMismatchException x)
            {
                System.out.println("I'm sorry but that is an invalid input" +
                        "\nYou will be redirected to the main menu shortly..."+
                        "\n");
                goodput = false;
            }
        } while (goodput == true);
    }
}

最佳答案

问题是您如何更新值。在您的方法中,您将得到如下所示的内容:

double deposit = 0.00d;
double balance = 250.00d;


在此处,请注意如何将double关键字放在变量之前。这样,您要告诉Java在本地范围内创建一个名为depositbalance的新变量。

如果要修改名为depositbalance的字段,则肯定必须删除它们前面的double关键字,并可以选择将this.放在变量前面。它看起来像以下内容:

deposit = 0.00d;
balance = 250.00d;


要么

this.deposit = 0.00d;
this.balance = 250.00d;


您将像这样替换变量withdrawbalancedeposit的所有实例,但仅替换声明。

不需要指定类型的原因是因为您已经在字段声明中定义了它。因此,当您再次在方法中定义它时,就是说要使用该名称创建一个新变量并在当前作用域中键入。

编辑:
正如Suyash Limaye所说,您将必须从方法中删除static关键字。使用static关键字会阻止方法访问非静态字段,这将导致冲突。

10-07 16:47