java - 在多个数组中调整值

标签 java arrays

当我更新数组中某些记录的余额时,使用存款和取款金额,该特定记录的余额会随着数组中记录的余额而变化。

如何解决?

private String name;
private int pin;
private int account;
private static double balance;

public void setBalance(double amount)
{
    balance = amount;
}

public static void deposit(double aDeposit) 
{ 
balance = balance + aDeposit; 
} 
public static void withdraw(double aWithdraw) 
{ 
    if  
    ( balance >= aWithdraw) 
        balance = balance - aWithdraw; 
    else if 
    ( balance < aWithdraw) 
        System.out.println("Cannot withdarw amount."); 
}

public double getBalance( )
{
    return balance;
}
public boolean equal(CustomerRecord otherObject)
{
    return (name.equalsIgnoreCase(otherObject.name) &&
           (pin == otherObject.pin) &&
           (account == otherObject.account) &&
           (balance == otherObject.balance));

      }
  }

    do{   


        System.out.println("Enter the name");
        String aName;
        Scanner keyboard = new Scanner(System.in);
        aName = keyboard.nextLine();

        System.out.println("Enter the pin");
        int aPin;
        aPin = keyboard.nextInt();


             for

             (int i = 0; i < index; i++) {
              CustomerRecord record = anotherArray[i];
              if
              ((record.getName().equalsIgnoreCase(aName)) && (record.getPin() == (aPin)))

          {
                 System.out.println(record);

             System.out.println("Enter the amount you wish to Deposit");
             double aDeposit;
             aDeposit = keyboard.nextDouble();
             CustomerRecord.deposit(aDeposit);

             System.out.println("Enter the amount you wish to withdraw");
             double aWithdraw;
             aWithdraw = keyboard.nextDouble();
             CustomerRecord.withdraw(aWithdraw);


            record.getBalance();

  }
             }

        System.out.println("\nAnother Transaction? (y for yes) (n for no)");
        repeat = keyboard.next().charAt(0);

    }
    while
        (

         repeat == 'y' || repeat == 'Y') ;

        //Print the records on screen

    { for (int i = 0; i < index; i++)
        System.out.print(anotherArray[i]);
    }

最佳答案

您没有显示您定义 balance 字段的位置,但事实上您可以从 static 方法 depositwithdraw 我猜它本身就是一个静态变量,比如说

private static double balance;

现在,static 在这里是什么意思?如果你弄清楚了这一点,你就会知道你的程序中的错误是什么,以及为什么在一个对象中改变它会改变所有对象

关于java - 在多个数组中调整值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9900068/

相关文章:

从 HTML 表单到 UPDATE MySQL 的 PHP 数组

php - 如何从 php 数组创建一个 javascript 数组?

Python的列表理解和其他更好的实践

java - 如何从 ViewPager 保存图像?

java - Struts2 - 转到无效的 URL 不会重定向到映射的 404 页面或给出默认的无操作消息

java - 在FileDialog中设置文件名

javascript - 根据是/否将 Controller 中的项目保存到数组中

C数组在被函数修改后被破坏

java - 如何查找并消除 Java 程序中的随机性来源

java - 如何在swt组合框中显示最大数量的数据?