我正在为贷款系统创建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

最佳答案

每次创建新数组时,都将删除前一个数组(当然,它不会被删除,但是对它的所有引用都会丢失,因此它也可能已被删除)。更好的解决方案是使用ArrayList<Loan>而不是数组,*在循环之前创建一次,然后根据需要简单地将项目添加到列表中。将ArrayList视为可以动态扩展或缩小大小的数组。

否则,如果必须使用数组,则考虑在声明数组时创建一次数组,并在创建数组时为其提供足够的项,以免空间不足。

关于java - switch语句期间的增量计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20036759/

10-13 03:43