如何在我的方法中访问我的数组?

时间:2013-11-22 06:10:10

标签: java arrays methods

这是我的问题。我正在尝试在我的main中构建一个HomeLoan数组,然后从我的main,从我的main调用printHLoans(hLoans),以便我可以列出它们然后添加更多功能。不幸的是,它说无法将hLoan解析为变量。

主类

import java.util.InputMismatchException;
import java.util.Scanner;
public class LoanTest 
{
static int term;
static int loanID=0;
static int hHowMany=0;
static int cHowMany=0;
public static void main(String[] args) 
{
    Scanner input = new Scanner (System.in);
    System.out.print("Enter name: ");
    String name = input.nextLine();


    System.out.print("Enter Customer ID: ");
    int custID=0;
    do
    {
        try 
        {
            custID = input.nextInt();
        }
        catch (InputMismatchException e) 
        {
            System.out.println("Customer IDs are numbers only");
            input.next();
        }
    }while (custID<1);   
    boolean aa=true;
    while(aa)
    {
        System.out.println("Do you have a (h)ome loan or a (c)ar loan?");
        String a = input.next();
        switch (a) 
        {
        case "h": System.out.println("You selected Home Loan");
                System.out.println("How many Home Loans would you like to enter?");
                hHowMany = input.nextInt();
                HomeLoan[] hLoans = new HomeLoan[hHowMany];
                int z=0;
                while (hHowMany>z)
                {

                hLoans[z] = new HomeLoan(name, custID);
                z++;
                }
                break;
        case "c": System.out.println("You selected Car Loan");
                System.out.println("How many Car Loans would you like to enter?");
                cHowMany = input.nextInt();
                int x=0;
                CarLoan[] carLoans = new CarLoan[cHowMany];
                while (x<cHowMany)
                {
                    System.out.print("Enter Loan ID: ");
                    loanID=0;
                    do
                    {
                        try
                        {
                            loanID=input.nextInt();
                        }
                        catch (InputMismatchException e)
                        {
                            System.out.println("Loan IDs are number only");
                            input.next();
                        }
                    }while (loanID<1);
                    boolean b=true;
                    while(b)
                    {

                        System.out.print("Enter term: ");
                        term=input.nextInt();
                        boolean t = CarLoan.termCorrect(term);
                        if(t){System.out.println("Error: Maximum of 6 year");}
                        else {b=false; System.out.println("You entered: " + term + " years");}
                    }
                carLoans[x] = new CarLoan(name, custID, loanID, term);
                x++;
                }
                break;
        default: System.out.println("Invalid input");
                break;
        }
        System.out.print("Would you like to enter another loan?");
        System.out.print("(y)es or (n)o:");
        String m = input.next();
        System.out.println("");
        switch (m)
        {
        case "y": break;
        case "n": aa=false;
                  break;
        default: System.out.print("Invalid entry");
        }
    }
    printHLoans(hLoans);

    System.out.println("Thank you for using Loan Software");
    System.out.println("Have a nice day");
}
public static void printHLoans(HomeLoan hLoans[])
{
    System.out.println("Here are the loans you entered . . .");
    int zzz=0;
    while (zzz<hHowMany)
    {
        term = hLoans[zzz].getTerm();
        loanID=hLoans[zzz].getLoanID();
        String address=hLoans[zzz].getAddress();
        double loanAmount = hLoans[zzz].getLoanAmount();
        System.out.print(zzz + ": ");
        System.out.print(hLoans[zzz].toString(loanID, address, term, loanAmount));
        System.out.println();
        zzz++;
    }
}
}

HomeLoan Class

 import java.util.InputMismatchException;
 import java.util.Scanner;
 public class HomeLoan extends Loan
 {
private String address;
int howMany;
private double loanAmount;
private int term;
private int loanID;

public HomeLoan()
{

}
public HomeLoan(String name, int custID)
{
    //super(name, custID, loanID);
    Scanner input = new Scanner (System.in);
    System.out.print("Enter Loan ID: ");
    loanID=0;
    do
    {
        try
        {
            loanID=input.nextInt();
        }
        catch (InputMismatchException e)
        {
            System.out.println("Loan IDs are number only");
            input.next();
        }
    }while (loanID<1);
    boolean l=true;
    while (l)
    {

        System.out.print("Enter term: ");
        term=input.nextInt();
        boolean s = HomeLoan.termCorrect(term);
        if (s){System.out.println("Error: Maximum of 30 years");}
        else {l=false;}
    }
    //System.out.println();
    input.nextLine();
    System.out.print("Enter your address: ");
    address=input.nextLine();
    System.out.print("Enter loan ammount: ");
    loanAmount = input.nextDouble();
    double annualInterest = 10.99;
    double monthlyPayment = amortization(loanAmount, annualInterest, term);
    double newBalance=payment(monthlyPayment, loanAmount);
    //System.out.println("Payment: " + monthlyPayment);
    //System.out.println("New balance after payment: " + newBalance);

}
public static boolean termCorrect(int term)
{
    boolean a;
    if (term>30) {a=true; return a;}
    else {a=false; return a;}
}
public String toString(String name, int custID, int loanID)
{
    String display = "Name: " + name + " Customer ID: " + custID + " Address: "+ address+ " Loan ID: " + loanID + " Loan Amount: $" + loanAmount
            + " Current Balance: $";
    return  display;
}
public String getAddress()
{
    return address;
}
public double getLoanAmount()
{
    return loanAmount;
}
public int getTerm()
{
    return term;
}
public int getLoanID()
{
    return loanID;
}
public String toString(int loanID, String address, int term, double loanAmmount)
{
    String displa =  "ID: " + loanID + " Address:" + address + " Term: " + term + " Loan Ammount: " + loanAmmount;
    return displa;

}
private void equals()
{

}
public void printHLoans(HomeLoan hLoans[], int xxx)
{

}

}

我如何使这项工作?

1 个答案:

答案 0 :(得分:1)

那是因为您已在HomeLoan[] hLoans = new HomeLoan[hHowMany];循环中声明了while,并在此之后尝试访问它。这就是为什么一旦while循环结束,hLoans超出范围。在您的情况下,更具体地说,它在case "h"的{​​{1}}范围内,这进一步缩小了switch范围内的范围。

为了能够在hLoans之外访问它,您需要在高于while的范围内声明它。做这样的事。

while