異なる金利投資の増加

2415 ワード

package test;

import java.text.*;

public class CompoundInterest {

	public static void main(String[] args) {
		final int STARTRATES = 10;
		final int NRATES = 6;
		final int NYEARS = 10;
		
		//set interest rates to 10 ... 15%
		double[] interestRate = new double[NRATES];
		for (int i = 0; i < interestRate.length; i++)
		{
			interestRate[i] = (STARTRATES + i) / 100.0;
		}
		
		double[][] balance = new double[NYEARS][NRATES];
		
		//set initial balance( )to 1000 
		for (int j = 0; j < balance[0].length; j++)
		{
			balance[0][j] = 10000;
		}
		
		//compute interest for future years
		for (int i = 1; i < balance.length; i++)
		{
			for (int j = 0; j < balance[i].length; j++)
			{
				//get last year's balance from previous row
				double oldBalance = balance[i-1][j];
				
				//compute interest
				double interest = oldBalance * interestRate[j];
				
				//compute this year's balance
				balance[i][j] = oldBalance + interest;
			}
		}
		
	
		// print one row of interest rates
		
		NumberFormat formatter = NumberFormat.getPercentInstance();
		
		for (int j=0; j < interestRate.length; j++)
		{
			System.out.print("	");
			System.out.print(formatter.format(interestRate[j]) + "	");
		}
		System.out.println();
		
		//print balance table
		
		formatter = NumberFormat.getCurrencyInstance();
	    for (int i=0; i< balance.length; i++)
	    {
	    	//print table row
	    	for (int j=0; j < balance[i].length; j++)
	    	{
	    		System.out.print("	");
	    		System.out.print(formatter.format(balance[i][j]));
	    	}
	    	System.out.println();
	    }
	}
}

出力結果:
	10%		11%		12%		13%		14%		15%	
	¥10,000.00	¥10,000.00	¥10,000.00	¥10,000.00	¥10,000.00	¥10,000.00
	¥11,000.00	¥11,100.00	¥11,200.00	¥11,300.00	¥11,400.00	¥11,500.00
	¥12,100.00	¥12,321.00	¥12,544.00	¥12,769.00	¥12,996.00	¥13,225.00
	¥13,310.00	¥13,676.31	¥14,049.28	¥14,428.97	¥14,815.44	¥15,208.75
	¥14,641.00	¥15,180.70	¥15,735.19	¥16,304.74	¥16,889.60	¥17,490.06
	¥16,105.10	¥16,850.58	¥17,623.42	¥18,424.35	¥19,254.15	¥20,113.57
	¥17,715.61	¥18,704.15	¥19,738.23	¥20,819.52	¥21,949.73	¥23,130.61
	¥19,487.17	¥20,761.60	¥22,106.81	¥23,526.05	¥25,022.69	¥26,600.20
	¥21,435.89	¥23,045.38	¥24,759.63	¥26,584.44	¥28,525.86	¥30,590.23
	¥23,579.48	¥25,580.37	¥27,730.79	¥30,040.42	¥32,519.49	¥35,178.76