复利计算C转java版

  1 import java.util.Scanner;
  2 
  3 public class Compound_int {
  4 
  5     public static void main(String[] args) {
  6         tip();
  7         while (true) {
  8             menu();
  9             int n = scanner.nextInt();
 10             if (n >= 0 && n <= 7) {
 11                 if (n == 0)
 12                     break;
 13                 switch (n) {
 14                 case 1:
 15                     benjin();
 16                     break;
 17                 case 2:
 18                     fuli();
 19                     break;
 20                 case 3:
 21                     danli();
 22                     break;
 23                 case 4:
 24                     years();
 25                     break;
 26                 case 5:
 27                     annual_int_rate();
 28                     break;
 29                 case 6:
 30                     YM_Equal_cast_final();
 31                     break;
 32                 case 7:
 33                     Average_capital_plus_interest();
 34                     break;
 35                 case 0:
 36                     n = 0;
 37                     break;
 38 
 39                 }
 40             } else {
 41                 System.out.println("输入超出范围,请重新输入:");
 42 
 43             }
 44         }
 45     }
 46 
 47     static Scanner scanner = new Scanner(System.in);
 48 
 49     static void tip()// 计算公式和一些变量的介绍
 50     {
 51         System.out.printf("复利计算软件\n");
 52         System.out.printf("    复利计算公式:F=P*pow((1+i/m),N*m)\n");
 53         System.out.printf("    单利计算公式:F=P+P*N*i\n");
 54         System.out.printf("    等额投资公式:F=P*12*(1+i)*(pow(1+i,N)-1)/i\n");
 55         System.out.printf("\tF:复利终值|年金终值\n");
 56         System.out.printf("\tP:本金\n");
 57         System.out.printf("\ti:利率\n");
 58         System.out.printf("\tN:利率获取时间的整数倍\n");
 59         System.out.printf("\tm:年复利次数(一年当中的滚利次数)\n");
 60     }
 61 
 62     static void menu()// 菜单
 63     {
 64         System.out.printf("\t\t|-----------------------------------|\n");
 65         System.out.printf("\t\t|             welcome               |\n");
 66         System.out.printf("\t\t|-----------------------------------|\n");
 67         System.out.printf("\t\t|           1:计算本金               |\n");
 68         System.out.printf("\t\t|           2:计算年复利终值          |\n");
 69         System.out.printf("\t\t|           3:单利计算               |\n");
 70         System.out.printf("\t\t|           4:计算年份               |\n");
 71         System.out.printf("\t\t|           5:计算年利率             |\n");
 72         System.out.printf("\t\t|           6:等额定投(年|月)         |\n");
 73         System.out.printf("\t\t|           7:等额还款               |\n");
 74         System.out.printf("\t\t|           0:结束                   |\n");
 75         System.out.printf("\t\t|-----------------------------------|\n");
 76         System.out.printf("请输入你要选择的功能(0~7):");
 77 
 78     }
 79 
 80     static double capital_formula(double F, double i, int N, int m)// 本金计算公式
 81     {
 82         double P;
 83         P = F / Math.pow((1 + i / m), N * m);
 84         return P;
 85     }
 86 
 87     static void benjin()// 计算本金
 88     {
 89         int N, m;
 90         double i, F, P;
 91         System.out.printf("复利终值:");
 92         F = scanner.nextDouble();
 93         System.out.printf("年利率:");
 94         i = scanner.nextDouble();
 95         System.out.printf("存入年限:");
 96         N = scanner.nextInt();
 97         System.out.printf("年复利次数:");
 98         m = scanner.nextInt();
 99         P = capital_formula(F, i, N, m);
100         System.out.println("年复利终值为" + F + "需要本金为:" + P);
101     }
102 
103     static double compound_amount_formla(double P, double i, int N, int m)// 复利计算公式
104     {
105         double F;
106         F = P * Math.pow((1 + i / m), N * m);
107         return F;
108     }
109 
110     static void fuli()// 计算复利终值
111     {
112         int N, m;
113         double i, F, P;
114         System.out.printf("存入本金:");
115         P = scanner.nextDouble();
116         System.out.printf("年利率:");
117         i = scanner.nextDouble();
118         System.out.printf("存入年限:");
119         N = scanner.nextInt();
120         System.out.printf("年复利次数:");
121         m = scanner.nextInt();
122         F = compound_amount_formla(P, i, N, m);
123         System.out.println("复利终值:" + F);
124     }
125 
126     static double simple_interest_formula(double P, int N, double i)// 单利计算公式
127     {
128         double F;
129         F = P + P * N * i;
130         return F;
131     }
132 
133     static void danli()// 单利计算
134     {
135         int N;
136         double i, F, P;
137         System.out.printf("存入本金:");
138         P = scanner.nextDouble();
139         System.out.printf("年利率:");
140         i = scanner.nextDouble();
141         System.out.printf("存入年限:");
142         N = scanner.nextInt();
143         F = simple_interest_formula(P, N, i);
144         System.out.println("本息和为:" + F);
145     }
146 
147     static int years_formula(double F, double P, double i, int m)// 根据复利终值求年份
148     {
149         int year;
150         year = (int) (Math.log(F / P) / Math.log(1 + i / m) / m);
151         return year;
152     }
153 
154     static void years()// 求年份
155     {
156         int N, m;
157         double i, F, P;
158         System.out.printf("复利终值:");
159         F = scanner.nextDouble();
160         System.out.printf("存入本金:");
161         P = scanner.nextDouble();
162         System.out.printf("年利率:");
163         i = scanner.nextDouble();
164         System.out.printf("年复利次数:");
165         m = scanner.nextInt();
166         N = years_formula(F, P, i, m);
167         System.out.println("从" + P + "到" + F + "需要" + N + "年");
168     }
169 
170     static double rate_formula(int m, double F, double P, int N)// 复利的利率计算公式
171     {
172         double rate;
173         rate = m * (Math.pow(F / P, 1.0 / (N * m)) - 1);
174         return rate;
175     }
176 
177     static void annual_int_rate()// 计算年利率
178     {
179         int N, m;
180         double i, F, P;
181         System.out.printf("复利终值:");
182         F = scanner.nextDouble();
183         System.out.printf("存入本金:");
184         P = scanner.nextDouble();
185         System.out.printf("存入年限:");
186         N = scanner.nextInt();
187         System.out.printf("年复利次数:");
188         m = scanner.nextInt();
189         i = rate_formula(m, F, P, N);
190         System.out.println("从" + P + "到" + F + "需要" + i);
191     }
192 
193     static double according_year_formula(double P, int N, double i)// 按照年份等额投资计算公式
194     {
195         double final_value;
196         final_value = P * (Math.pow(1 + i, N) - 1) / i;
197         return final_value;
198     }
199 
200     static double according_month_formula(double P, int N, double i)// 按照月份等额投资计算公式
201     {
202         double final_value;
203         final_value = P * 12 * (1 + i) * (Math.pow(1 + i, N) - 1) / i;
204         return final_value;
205     }
206 
207     static void YM_Equal_cast_final()// 计算等额投资
208     {
209         int N, n;
210         double i, F, P;
211         System.out.printf("\t\t1:按年投资\n\t\t2:按月投资\n");
212         System.out.printf("请选择你要的功能<1|2>:");
213         n = scanner.nextInt();
214         if (n == 1) {
215             System.out.printf("存入本金:");
216             P = scanner.nextDouble();
217             System.out.printf("存入年限:");
218             N = scanner.nextInt();
219             System.out.printf("年利率:");
220             i = scanner.nextDouble();
221             F = according_year_formula(P, N, i);
222             System.out.println(N + "年后的总产值:" + F);
223 
224         } else if (n == 2) {
225             System.out.printf("存入本金:");
226             P = scanner.nextDouble();
227             System.out.printf("存入年限:");
228             N = scanner.nextInt();
229             System.out.printf("年利率:");
230             i = scanner.nextDouble();
231             F = according_month_formula(P, N, i);
232             System.out.println(N + "年后的总产值:" + F);
233         } else {
234             System.out.printf("输入有误!\n");
235         }
236 
237     }
238 
239     static double Average_capital_plus_interest_formula(double F, int N,double i)// 等额还款公式
240     {
241         double refund;
242         refund = F * i / (12 * (1 + i) * (Math.pow(1 + i, N) - 1));
243         return refund;
244     }
245 
246     static void Average_capital_plus_interest()// 等额还款
247     {
248         int N;
249         double i, F, P;
250         System.out.printf("贷款金额:");
251         F = scanner.nextDouble();
252         System.out.printf("存入年限:");
253         N = scanner.nextInt();
254         System.out.printf("年利率:");
255         i = scanner.nextDouble();
256         P = Average_capital_plus_interest_formula(F, N, i);
257         System.out.println("贷款" + F + "每月需要还款" + P);
258     }
259 
260 }

 

posted @ 2016-03-26 19:00  douidoui  阅读(406)  评论(0编辑  收藏  举报