Java 如何在方法中访问数组?
这是我的问题。我试图在main中构建一个HomeLoan数组,然后从main调用printhloan(hloan),这样我就可以列出它们,然后添加更多功能。不幸的是,它说hLoan不能作为变量来解析 主类Java 如何在方法中访问数组?,java,arrays,methods,Java,Arrays,Methods,这是我的问题。我试图在main中构建一个HomeLoan数组,然后从main调用printhloan(hloan),这样我就可以列出它们,然后添加更多功能。不幸的是,它说hLoan不能作为变量来解析 主类 import java.util.InputMismatchException; import java.util.Scanner; public class LoanTest { static int term; static int loanID=0; static int hHowMa
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++;
}
}
}
import java.util.InputMismatchException;
导入java.util.Scanner;
公共级借调测试
{
静态整数项;
静态int loanID=0;
静态int hHowMany=0;
静态int=0;
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
系统输出打印(“输入名称:”);
字符串名称=input.nextLine();
系统输出打印(“输入客户ID:”);
int custID=0;
做
{
尝试
{
custID=input.nextInt();
}
捕获(输入不匹配异常e)
{
System.out.println(“客户ID仅为数字”);
input.next();
}
}while(custIDz)
{
hLoans[z]=新住房贷款(名称、客户ID);
z++;
}
打破
案例“c”:System.out.println(“您选择了汽车贷款”);
System.out.println(“您希望输入多少汽车贷款?”);
cHowMany=input.nextInt();
int x=0;
CarLoan[]carLoans=新CarLoan[cHowMany];
while(x这是因为您在while
循环中声明了homeloans[]hLoans=new HomeLoan[hHowMany];
,然后试图访问它。这就是为什么一旦while
循环结束,hLoans
就超出了范围。在您的例子中,它更具体地说是在案例“h”中开关的
,这进一步缩小了hLoans
的范围,使其仅在其中
为了能够在while
之外访问它,您需要在高于while
的范围内声明它
HomeLoan[] hLoans; // Before while
while(aa) {
...
switch..
case "h": hLoans = new HomeLoan[hHowMany];
}
printHLoans(hLoans);
没有办法从那里传递吗?您可以从案例本身内部传递,但这取决于您的要求。最好的方法是在外部声明它,并在需要时初始化它,然后再传递,就像您已经做的那样。不会完全按照我的意图进行传递,但它会起作用。非常感谢
HomeLoan[] hLoans; // Before while
while(aa) {
...
switch..
case "h": hLoans = new HomeLoan[hHowMany];
}
printHLoans(hLoans);