我正在为贷款系统创建HomeLoans和CarLoans。需要能够创建用户想要的数量。 switch语句用于菜单。我目前的问题是,当我创建一个家庭娱乐并返回菜单,然后创建另一个,它迫使我发2贷款。我猜是因为我的Homecount ++。然后,当我查看贷款时,它只会记住前两个而不是第一个。如何继续创建贷款并将其添加到数组中,而不会丢失进出switch语句的数据?
public class TestingClasses {
private static Loan getInfo(Scanner scan, char c){
System.out.println("Please enter your Name: ");
String nameOf = scan.next();
System.out.println("Please enter an ID Number: ");
String id_Number = scan.next();
System.out.println("Please enter a Loan Number: ");
String loan_Number = scan.next();
System.out.println("Please enter the amount of the Loan: ");
double loan_Amount = scan.nextDouble();
System.out.println("Please enter your desired Interest Rate: ");
double interest_rate = scan.nextDouble();
while(interest_rate > 100.00){ //Check valid number
System.out.println("Invalid percentage. Please enter 0.00 - 100.00");
interest_rate = scan.nextDouble();
}
System.out.println("How long will you like the loan? Please enter 1-30.");
int term = scan.nextInt();
while(term > 30){ // Check for valid number
System.out.println("Invalid number of years. Please enter 1-30.");
term = scan.nextInt();
}
System.out.println("Current balance is being set to the Loan amount. \n");
double c_balance = loan_Amount;
if(c == 'H'){
System.out.println("Please enter an address for the Home: ");
String address = scan.next();
return new HomeLoan(nameOf,id_Number,loan_Number,loan_Amount,
interest_rate,term,c_balance,address);
}
if(c == 'C'){
System.out.println("Please enter the Make of the car: ");
String make = scan.next();
System.out.println("Please enter the Model of the car: ");
String model = scan.next();
System.out.println("Please enter the Year of the car: ");
String year = scan.next();
return new CarLoan(nameOf,id_Number,loan_Number,loan_Amount,
interest_rate,term,c_balance,make,model,year);
}
else
return null;
}
public static void main(String[] args) throws IOException {
int CarCount=0,HomeCount = 0;
int j = 0;
HomeLoan HL[]=null;
CarLoan CL[]=null;
Scanner scan = new Scanner(System.in);
int menuChoice = 0;
char c;
HL= new HomeLoan[60];
String mainMenu = ("Select a choice from the menu: \n"
+ "1. Create HomeLoan\n"
+ "2. Create CarLoan\n"
+ "3. Make Payment\n"
+ "4. Check Balances\n"
+ "5. List of HomeLoans\n"
+ "6. List of CarLoans\n"
+ "7. Amortization Report\n"
+ "8. Exit");
System.out.println("Welcome to the Loan system! \n");
do{
System.out.println(mainMenu);
menuChoice = scan.nextInt();
while (menuChoice < 1 || menuChoice > 8) {
System.out.print("\nError! Incorrect choice.\n");
System.out.println(mainMenu);
menuChoice = scan.nextInt();
}
switch (menuChoice) {
case 1: {
c = 'H';
// Add 1 to HomeCount
for(; j == HomeCount; j++){
HL[j]=(HomeLoan)getInfo(scan,c);
System.out.println(HL[j].getHomeLoanNumber());
}
HomeCount++;
break;
}
case 2: {
c = 'C';
CarCount++; // Add 1 to Car Count
CL = new CarLoan[CarCount];
for(int j = 0; j < CarCount; j++){ //Loop through loans
CL[j]=(CarLoan)getInfo(scan,c);
System.out.println(CL[j].getCarLoanNumber());
}
break;
}
case 5: {
for(int k=0; k < HomeCount; k++){
System.out.println(HL[k]+ "\n");
}
break;
}
}
}while(menuChoice != 8); //ending do while loop
}//end main
}//end class
答案 0 :(得分:2)
每次创建新数组时,都会删除以前的数组(好吧,它不会被删除,但是对它的所有引用都会丢失,所以它也可能已被删除)。更好的解决方案是使用ArrayList<Loan>
而不是数组,在循环之前创建一次*,然后根据需要简单地将项添加到List。将ArrayList视为可以动态扩展或缩小的数组。
否则,如果你必须使用数组,那么在你声明它时考虑创建一个数组,并在创建它时给它足够的项目,这样你就不会用完空间。 / p>
答案 1 :(得分:1)
每次到那里时,您都会在交换机内部将新数组重新分配到HL
和CL
。因此,它只记得上次迭代的贷款。