2013-11-22 6 views
-1

내 문제는 여기에 있습니다. 내 메인에 HomeLoan의 배열을 만들고 내 메인에서 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 클래스는

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

HomeLoan[] hLoans = new HomeLoan[hHowMany];while 루프 내에 신고했으며 이후에 액세스하려고했기 때문입니다. 그렇기 때문에 while 루프가 끝나면 hLoans이 범위를 벗어납니다. 귀하의 경우에는 더 구체적으로는 의 switchcase "h" 내에 있으며 hLoans의 범위를 더 좁혀 있습니다.

while 외부에서 액세스하려면 while보다 큰 범위에서 선언해야합니다. 이런 식으로하십시오.

HomeLoan[] hLoans; // Before while 
while(aa) { 
... 
    switch.. 
    case "h": hLoans = new HomeLoan[hHowMany]; 
} 
printHLoans(hLoans); 
+0

거기에서 전달할 방법이 없습니까? – user1873736

+0

케이스 자체에서 전달할 수 있지만 요구 사항에 따라 다릅니다. 가장 좋은 방법은 방금 외부에서 선언하고 필요할 때마다 초기화 한 다음 잠시 후 전달하는 것입니다. – SudoRahul

+2

의도 한대로되지는 않지만 제대로 작동합니다. 엄청 고마워 – user1873736

관련 문제