2016-02-26 95 views
1

我创建了一种计算并显示基于年数的简单兴趣值的方法。我想告诉所有的年,所以例如,如果年是5,我想说明年1,2,3,4的值,5显示for循环的所有迭代

这是到目前为止我的代码

public static String numbers(String method, double principal, double rate, double years) { 

    double simpleInterest = principal + (principal * (rate/100) * years); 
    String interest = ""; 
    for (double digit = 1; digit <= years; digit++) { 
     if (method.equals("Simple")) { 
      interest = "Principal: $" + principal + ',' + " Rate: " + rate + "\n" + "Year Simple Interest Amount\n" 
        + digit + "-->$" + simpleInterest; 
     } 
    } 
    return interest; 
} 

public static void main(String[] args) { 

    System.out.print(numbers("Simple", 5000, 5, 5)); 
} 

我输出

Principal: $5000.0, Rate: 5.0 
Year Simple Interest Amount 
5.0-->$6250.0 

但我想它也显示了往年一样

Principal: $5000.0, Rate: 5.0 
Year Simple Interest Amount 
1.0-->$5250.0 
2.0-->$5500.0 
3.0-->$5750.0 
4.0-->$6000.0 
5.0-->$6250.0 

什么我需要做的,屏幕显示T他前几年?

+0

那你试试?我的意思是,不会说谎,你已经编码,只是没有打印。 – Fallenreaper

+0

首先你没有计算利息,但是如果没有付款,总欠款。所以我假设你在下面将'interest'重命名为'total'。你可以在计算循环中放置一个打印语句,或者你必须将年度总计作为一个列表返回,比如'totals = []'在循环前创建一个空列表,然后'totals.append(total) '循环内,最后'返回总计' – roadrunner66

回答

0

您必须计算simpleInterest每年的simpleInterest = principal + (principal * (rate/100) * digit);这里digit代表year

并且还使用+=运算符将结果连接到interestreturn它。 您也可以使用字符串concat()方法 以下是此方法的语法:public String concat(String s)。 你可以使用它像这样:interest = interest.concat(digit + "-->$" + simpleInterest + "\n");

public static String numbers(String method, double principal, double rate, double years) { 

     double simpleInterest = principal + (principal * (rate/100) * years); 
     String interest = ""; 
     System.out.println("Principal: $" + principal + ',' + " Rate: " + rate + "\n"+ "Year Simple Interest Amount\n"); 
     for (double digit = 1; digit <= years; digit++) { 
      if (method.equals("Simple")) { 
       simpleInterest = principal + (principal * (rate/100) * digit); 
       interest += digit + "-->$" + simpleInterest + "\n"; 
      } 
     } 
     return interest; 
    } 

    public static void main(String[] args) { 

     System.out.print(numbers("Simple", 5000, 5, 5)); 
    } 

输出:

Principal: $5000.0, Rate: 5.0 
Year Simple Interest Amount 

1.0-->$5250.0 
2.0-->$5500.0 
3.0-->$5750.0 
4.0-->$6000.0 
5.0-->$6250.0 
+1

谢谢。 + =运算符解决我的问题 –

0
if (method.equals("Simple")) { 
      interest = "Principal: $" + principal + ',' + " Rate: " + rate + "\n" + "Year Simple Interest Amount\n" 
        + digit + "-->$" + simpleInterest; 
     } 
在你上面的代码

被重新分配interest如果method等于"simple"。因此,返回最后分配的值。

使用+=将新结果连接到interest或仅返回String[],其中包含每个结果作为单个元素,然后遍历它。

0

公共静态字符串编号(字符串方法,双重委托,双倍速率,双年) {

 String interest = ""; 
     for (double digit = 1; digit <= years; digit++) 
     { 
      if (method.equals(Simple)) 
      { 
       double simpleInterest = principal + (principal * (rate/100) * digit); 
       interest += "Principal: $" + principal + ',' + " Rate: " + rate + "\n" + "Year Simple Interest Amount\n" 
         + digit + "-->$" + simpleInterest;     
      } 
     } 
     return interest; 
    } 
+0

您需要每年使用“+ =”附加到字符串兴趣。否则,它会将其更改为最新的。而且你需要在double simpleInterest中改变一年,否则simpleInterest总是$ 6,250。 – David