java - 如何存储和修改账户余额:

标签 java

我有一个小账户,可以在其中存款、取款、查看余额和退出。一切都运行得很完美,但是我希望根据我采取的方法(取款、存款等)来调整余额。我从 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");    
    }
}

类(class):

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 关键字会阻止方法访问非静态字段,从而导致冲突。

关于java - 如何存储和修改账户余额:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56022460/

相关文章:

java - 使用事件移除卡片

java - 在我的 Mac 上使用 Eclipse 和 Ant/Junit 时很容易耗尽内存

java - 上传前获取post参数

java - AWT 和 Oracle Secure Global Desktop

java - 如何使用 hasprevious() 方法使用 java 反转单词?

java - Jenkins作业找不到JDK但Jenkins中配置了JDK

java - Jetty Java EE Executors服务

java - 如何管理自定义 CSS?

java - 谷歌地图 API v2 崩溃

java - BlockingQueue 的 drainTo() 方法的线程安全