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