Java_Fill getter 数组

Java_Fill an array by getters

尝试使用 getter 填充数组,但问题是数组中充满了相同的数字,如果第二个输入中的值发生变化,空洞数组将再次发生变化:

public class Payment {

Scanner kb = new Scanner(System.in);
private int operationNum = 0;
private int subOperationNum = 0;
private double reMoney = 0;

public int getOperationNum() {
    return operationNum;
}

public int getSubOperationNum() {
    return subOperationNum;
}

public double getReMoney() {
    return reMoney;
}

public void setOperationNum(int operationNum) {
    this.operationNum = operationNum;
}

public void setSubOperationNum(int subOperationNum) {
    this.subOperationNum = subOperationNum;
}

public void setReMoney(double reMoney) {
    this.reMoney = reMoney;
}

public void operationsLoop() {
    double [] a = new double[17];
    do {
        System.out.println("\n1- Cash");
        System.out.println("2- Car");
        System.out.println("3- Clothing");
        System.out.println("4- Credit Card");
        System.out.println("5- Food");
        System.out.println("6- Education");
        System.out.println("7- Electronics");
        System.out.println("8- Groceries");
        System.out.println("9- Health & Fitness");
        System.out.println("10- Medical");
        System.out.println("11- Travel");
        System.out.println("12- Utilities");
        System.out.println("13- Finish");
        System.out.print("\nEnter the number of operation : ");
        this.setOperationNum(kb.nextInt());
        this.operation2();
        this.operation12();
        this.collectReMoney();
        **for(int i = 0; i<a.length;i++){
            a[i] = this.getReMoney();
            System.out.print(a[i] + ", ");
        }**
    } while (operationNum < 13);
}

public void operation2() {
    if (this.getOperationNum() == 2) {
        System.out.println("\t1- Gas");
        System.out.println("\t2- Repair");
        System.out.println("\t3- Monthly Payment");
        System.out.print("\nEnter your chose : ");
        this.setSubOperationNum(kb.nextInt());
    }
}

public void operation12() {
    if (this.getOperationNum() == 12) {
        System.out.println("\t1- Electricity");
        System.out.println("\t2- Gas");
        System.out.println("\t3- Telephone");
        System.out.println("\t4- Water");
        System.out.print("\nEnter your chose : ");
        this.setSubOperationNum(kb.nextInt());
    }
}


public void collectReMoney() {
    if (this.getOperationNum() == 1) {
        System.out.print("Withdraw = ");
        this.setReMoney(kb.nextDouble());
    } else if (this.getOperationNum() == 4 || (this.getOperationNum() == 2 && this.getSubOperationNum() == 3)) {
        System.out.print("Payment = ");
        this.setReMoney(kb.nextDouble());
    } else if (this.getOperationNum() == 9 || this.getOperationNum() == 10) {
        System.out.print("Pay = ");
        this.setReMoney(kb.nextDouble());
    } else if (this.getOperationNum() != 13) {
        System.out.print("Spend = ");
        this.setReMoney(kb.nextDouble());
    }
}

如果我添加到数组循环 "this.setReMoney(0);" 只有第一个值改变

当用户输入一个值时,我想根据操作编号将其插入到数组中,其他操作的其余值应为零

在 for 循环中,您为数组的每个元素分配相同的数字,以避免您应该在 for 循环之外进行分配操作:

a[operationNum - 1] = this.getReMoney();
for(int i = 0; i<a.length;i++){
    System.out.print(a[i] + ", ");
}

此外,确保检查 ArrayIndexOutOfBoundsException 以防用户选择的选项编号大于数组的大小。