返回循环java的开始

时间:2016-01-21 16:54:51

标签: java loops while-loop

这是一个允许用户以年数输入贷款金额和贷款期限的程序,并显示每个利率的月付款和总付款,从5%到8%,增量为1/8。 / p>

我可能过度沉迷于此,因为我对编程很陌生,但我太深了,想要弄明白这一点。

使用我当前的代码,第一行正确输出,显示费率,总计和每月。但是,之后代码只会继续输出最内层的循环。我需要回到循环的开始。如果你能指出我正确的方向,我们将不胜感激。

P.S。我知道我的填充不到位。我最担心的是首先将算法放在“纸上”,然后再担心它的美感。

package loancalc194;

import java.util.Scanner;

public class LoanCalc194 {

public static void main(String[] args) {

 //LOAN CALCULATOR 
 //user enters loan AMOUNT, loan PERIOD(years), 
 //output displays MONTHLY and TOTAL payments
 //^displayed per 1/8 increment from 5-8% interest

 Scanner in = new Scanner(System.in);

 //prompt user
 //retrieve loan amount and period
 System.out.print("Please enter the loan amount: ");   
 double amount = in.nextDouble();   
 System.out.print("Please enter the loan period (years): ");
 int period = in.nextInt();

 //INTEREST LOOP///////////////////////////////////////////////////

 double interest = .05000;
 double inc = 0.00125;
 interest = interest + inc;
 double monthly = 0;
 double total = 0;

 System.out.println("Interest Rate\t\t\tTotal Payment\tMonthly Payment");

 while (interest < .08){
     //interest = interest + inc;
     System.out.print(interest + "\t");   

     while (true){ 
        total = (amount * period) + (interest * amount);
        System.out.print("\t" + total + "\t\t");

        while (true) {
            monthly = ((total / period) / 12);
            System.out.println(monthly);
            //interest = interest + inc;
        }
     } 
 }

1 个答案:

答案 0 :(得分:0)

一个循环应该足以满足您的需求。

你的两个while(true)循环除了永远循环使用相同的值之外别无其他。

在下面的代码中, interest 会增加并在每个循环中计算新计算,直到 interest 达到最大值,这可能就是您所需要的。

 double interest = .05000;
 double inc = 0.00125;
 double monthly = 0;
 double total = 0;

 while (interest < .08){

     System.out.print(interest + "\t");   

     total = (amount * period) + (interest * amount);
     System.out.print("\t" + total + "\t\t");

     monthly = ((total / period) / 12);
     System.out.println(monthly);

     interest = interest + inc;

 }