无法通过类方法更改对象的值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法通过类方法更改对象的值相关的知识,希望对你有一定的参考价值。

我制作了一个名为BankAccount的类,该类具有一种用于从给定帐户中存入或提取一定余额的方法。当我在主方法中调用该方法时,它不会使用新余额更新帐户。该程序应该允许用户选择三个帐户之一,并确定他们是否要从该银行帐户中存款或提款。然后它将询问他们是否愿意进行更多交易。如果他们不希望再进行任何交易,则可以选择在另一天进行交易,这将显示每个银行帐户的更新数据,而我的当前帐户不执行此操作,因为余额未从任一银行进行更新。存款或取款。任何帮助将不胜感激。

public class BankAccount
{

    public final int MIN_LENGTH = 2;
    public final int MAX_LENGTH = 40;
    public final String DEFAULT_NAME = "nobody";
    public final double MIN_BALANCE = 0.0;
    public final int DEFAULT_NUMBER = 0;
    public final int MIN_NUMBER = 0;
    public final int MAX_NUMBER = 999999;

    private double balance;
    private String owner;
    private int accountNumber;

    public BankAccount(double initBal, String name, int number)
    {
        balance = initBal;
        owner = name;
        accountNumber = number;
    }

    public BankAccount(double initBal, String name)
    {
        balance = initBal;
        owner = name;
        accountNumber = 45678;
    }

    public BankAccount(String name)
    {
        owner = name;
        balance = MIN_BALANCE;
        accountNumber = 34567;
    }

    public void deposit(double amount)
    {
        balance = balance + amount;
    }

    public void withdraw(double amount)
    {
        balance -= amount;
    }

    public void withdraw(double amount, double fee)
    {
        balance -= (amount + fee);
    }

    public double getBalance()
    {
        return balance;
    }

    public String getOwner()
    {
        return owner;
    }

    public int getAccountNumber()
    {
        return accountNumber;
    }

    public void setAccountNumber(int newAccountNumber)
    {
        accountNumber = newAccountNumber;
    }

    public void setOwner(String name)
    {
        owner = name;
    }

    public void display()
    {
        System.out.println("Account number: " + getAccountNumber());
        System.out.println("Account owner: " + getOwner());
        System.out.println("Account balance: " + getBalance() + "
");
    }

    public void close()
    {
        owner = "CLOSED";
        balance = 0.0;
    }

    private static boolean isValidName(String name)
    {
        final int MIN_LENGTH = 2;
        final int MAX_LENGTH = 40;

        if(name.length() >= MIN_LENGTH && name.length() <= MAX_LENGTH && name != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private static boolean isValidBalance(double initBalance)
    {
        if(initBalance > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private static boolean isValidNumber(int acctNum)
    {
        final int MIN_NUMBER = 0;
        final int MAX_NUMBER = 999999;

        if(acctNum >= MIN_NUMBER && acctNum <= MAX_NUMBER)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}


import java.util.Scanner;

public class Transaction
{

    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        int accountChoice;
        double moneyAmount;
        String accountAction, transactionChoice = "nothing";
        BankAccount accountOne = new BankAccount(1000.0,"Matthew Johnson",12345);
        BankAccount accountTwo = new BankAccount(1000.0,"Sue Alexander",45678);
        BankAccount accountThree = new BankAccount(1000.0,"James William",34567);

        System.out.println("The following accounts are available:
");
        do
        {
            accountOne.display();
            accountTwo.display();
            accountThree.display();

            System.out.print("Enter the number of the account you would like to access: ");
            accountChoice = keyboard.nextInt();

            System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
            accountAction = keyboard.nextLine();

            keyboard.nextLine();

            System.out.print("Enter the amount: ");
            moneyAmount = keyboard.nextDouble();

            if(accountChoice == 12345)
            {
                if(accountAction.equalsIgnoreCase("D"))
                {
                    accountOne.deposit(moneyAmount);
                }
                else if(accountAction.equalsIgnoreCase("W"))
                {
                    accountOne.withdraw(moneyAmount);
                }
            }
            else if(accountChoice == 45678)
            {
                if(accountAction.equalsIgnoreCase("D"))
                {
                    accountTwo.deposit(moneyAmount);
                }
                else if(accountAction.equalsIgnoreCase("W"))
                {
                    accountTwo.withdraw(moneyAmount);
                }
            }
            else if(accountChoice == 34567)
            {
                if(accountAction.equalsIgnoreCase("D"))
                {
                    accountThree.deposit(moneyAmount);
                }
                else if(accountAction.equalsIgnoreCase("W"))
                {
                    accountThree.withdraw(moneyAmount);
                }
            }

            keyboard.nextLine();
            System.out.println("More transactions? (y/n)");
            transactionChoice = keyboard.nextLine();

            if(transactionChoice.equalsIgnoreCase("Y"))
            {
                continue;
            }
            else if(transactionChoice.equalsIgnoreCase("N"))
            {
                System.out.println("Would you like to enter transactions for another day? (y/n)");
                transactionChoice = keyboard.nextLine();
                if(transactionChoice.equalsIgnoreCase("Y"))
                {
                    continue;
                }
                else if(transactionChoice.equalsIgnoreCase("N"))
                {
                    break;
                }
            }
            else
            {
                System.out.println("Invlid Input");
                break;
            }

        } while(!transactionChoice.equalsIgnoreCase("N"));
    }

}
答案

nextInt不会消耗缓冲区中剩余的换行符,该换行符是由(您的第一个)nextLine语句捕获的,并将accountAction设置为空字符串。通常,这就是为什么我不使用这种工作流程,而是依靠nextLine并分别解析该行的原因,例如...

System.out.print("Enter the number of the account you would like to access: ");
accountChoice = new Scanner(keyboard.nextLine()).nextInt();

System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
accountAction = keyboard.nextLine();

System.out.print("Enter the amount: ");
moneyAmount = keyboard.nextDouble();

[遇到此类问题时,请使用System.out.println打印您的变量,这样您就可以准确了解正在发生的事情,直到您了解如何使用调试器;)

您还可以通过使用一个或多个循环来改善代码,而不是依靠if-else语句,这将减少重复代码的数量,但这超出了问题的范围;)

以上是关于无法通过类方法更改对象的值的主要内容,如果未能解决你的问题,请参考以下文章

从片段函数更改 TextView 的值

Drupal 6 视图 2:PHP 片段

如何从活动中更改片段中视图的可见性

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

如何将应用程序对象放入片段类

Vue 无法更改嵌套 v-for 中的属性