首页 > 解决方案 > 为什么循环从对象数组中打印相同的东西?

问题描述

当我运行程序并输入几个抵押然后结束程序时,它会打印出整个对象数组,mortgageArray[],但由于某种原因,它只打印出您输入的最后一个抵押。不知道为什么有人可以帮助我?我认为问题可能出在将对象实例化到数组中时。

按揭类别

import java.util.*;
import java.lang.Math.*;
import java.text.DecimalFormat;

public class Mortgage {

    private static int loanAmount;
    private static int term;
    private static double interestRate;
    private static String accountNum;
    private static String lastName;
    private static double monthlyPayment;
    private static double totalPayment;

    public Mortgage(int loanAmount1, int term1, double interestRate1, String accountNum1){
        loanAmount = loanAmount1;
        term = term1;
        interestRate = interestRate1;
        accountNum = accountNum1;

    }

    public void promotional(){
        Mortgage m = new Mortgage();

        m.storeLastName();
        m.storeAccountNum();

        lastName = getLastName();
        accountNum = getAccountNum();

        monthlyPayment = m.calcMonthlyPayment();
        totalPayment = m.calcTotalPayment();

    }

    public void unique(){
        Mortgage m = new Mortgage();

        m.storeLastName();
        m.storeAccountNum();
        m.storeLoanAmount();
        m.storeInterestRate();
        m.storeTerm();

        monthlyPayment = m.calcMonthlyPayment();
        totalPayment = m.calcTotalPayment();
    }

    public Mortgage(){
        //dummy constructor
    }

    public static int getLoanAmount(){
        return loanAmount;
    }

    public void storeLoanAmount(){
        Scanner s = new Scanner(System.in);

        System.out.println("Enter the amount of the loan (Ex:75000): ");
        int loanAmount1 = s.nextInt();

        while(loanAmount1 < 75000 || loanAmount1 > 1000000){
            System.out.println("\tValid Loan Amounts are $75000-$1000000");
            System.out.println("\tPlease re-enter loan amount without $ or commas (Ex:75000): ");
            loanAmount1 = s.nextInt();  
        }
        loanAmount = loanAmount1;
    }

    public static int getTerm(){
        return term;
    }

    public void storeTerm(){
        Scanner s = new Scanner(System.in);

        System.out.println("Enter number of years for the loan: ");
        int term1 = s.nextInt();

        while(term1 < 10 || term1 > 40){
            System.out.println("\tValid Loan Terms are 10-40");
            System.out.println("\tPlease re-enter valid number of years: ");
            term1 = s.nextInt();    
        }
        term = term1;
    }

    public static double getInterestRate(){
        return interestRate;
    }

    public void storeInterestRate(){
        Scanner s = new Scanner(System.in);

        System.out.println("Enter yearly interest rate (Ex: 8.25): ");
        double interestRate1 = s.nextDouble();

        while(interestRate1 < 2 || interestRate1 > 7){
            System.out.println("\tValid Interest Rates are 2% - 7%");
            System.out.println("\tPlease re-enter valid yearly interest rate (Ex: 8.25): ");
            interestRate1 = s.nextDouble(); 
        }
        interestRate = interestRate1;

    }

    public String getLastName(){
        return lastName;
    }

    public void storeLastName(){
        Scanner s = new Scanner(System.in);

        System.out.println("Enter customer's Last Name Only: ");
        String lastName1 = s.nextLine();
        lastName = lastName1;
    }

    private double calcMonthlyPayment(){

        int months = term * 12;
        double monthlyInterest = (interestRate / 12 / 100);

        double monthlyPay = (loanAmount * monthlyInterest) / (1 - Math.pow(1+ monthlyInterest, -1 * months));

        return monthlyPay;
    }

    private double calcTotalPayment(){

        double totalPay = calcMonthlyPayment() * term * 12;

        return totalPay;
    }

    public String getAccountNum(){
        return accountNum;
    }

    public void storeAccountNum(){
        StringBuilder accountNum1 = new StringBuilder();
        Random rand = new Random();
        int accNum = rand.nextInt(9900);

        accNum += 100;

        accountNum1.append(lastName.substring(0,4));
        accountNum1.append(accNum);

        accountNum = accountNum1.toString();

    }

    public String toString(){
        DecimalFormat df = new DecimalFormat("#,###.00");
        String strMonthlyPayment = ("$" + df.format(calcMonthlyPayment()));
        String strTotalPayment = ("$" + df.format(calcTotalPayment()));

        return("Account Number: " + accountNum + "\nThe monthly payment is " + strMonthlyPayment + "\nThe total payment is " + strTotalPayment);

    }

}

MortgageApp 类

import java.util.*;

public class MortgageApp {


    public static void main(String [] args){
        Scanner s = new Scanner(System.in);
        Mortgage m = new Mortgage();

        int size = 10;

        Mortgage [] mortgageArray = new Mortgage [size];

        int index = 0;

        for(index = 0; index < size; index++){
            int choice;

            System.out.println("\nPlease choose from the following choices below:");
            System.out.println("\t1) Promotional Load (preset loan amount, rate, term)");
            System.out.println("\t2) Unique Loan (enter in loan values)");
            System.out.println("\t3) Quit (Exit the program)");
            System.out.println("\n\t Please enter your selection (1-3): ");

            choice = s.nextInt();

            while(choice < 1 || choice > 3){
                System.out.println("\t\tInvalid Choice. Please select 1, 2, or 3: ");
                choice = s.nextInt();
            }


            if(choice == 1){
                m.promotional();

                String accountNum1 = m.getAccountNum();

                mortgageArray[index] = new Mortgage(250000, 20, 3.2, accountNum1);
                System.out.println("PROMOTIONAL LOAN...:");
                System.out.println(mortgageArray[index].toString());
            }
            else if(choice == 2){
                m.unique();

                int loanAmount = m.getLoanAmount();
                int term = m.getTerm();
                double interestRate = m.getInterestRate();
                String accountNum1 = m.getAccountNum();

                mortgageArray[index] = new Mortgage(loanAmount, term, interestRate, accountNum1);
                System.out.println("UNIQUE LOAN...:");
                System.out.println(mortgageArray[index].toString());
            }

            else if(choice == 3){
                System.out.println("\nPROGRAM COMPLETE");
                System.out.println("Contents of Array...");

                for(int j = 0; j < 10; j++){
                    if(mortgageArray[j] != null){
                        System.out.println(mortgageArray[j].toString() + "\n");
                    }
                }
                index = 10;
            }

        }
    }       
}

标签: java

解决方案


问题在于你的循环最后

 for(int j = 0; j < 10; j++){
     if(mortgageArray[j] != null){
         System.out.println(mortgageArray[1].toString() + "\n");
     }
 }

它总是打印索引 1 处的元素。

应该

System.out.println(mortgageArray[j].toString() + "\n");

推荐阅读