代碼如下:
1 import java.util.Scanner;
2
3
4 public class compoundung {
5
6 /**
7 * @param args
8 */
9 public static void main(String[] args) {
10 // TODO Auto-generated method stub
11 int P = 0;
12 double i=0;
13 int n=0;
14 int F=0;
15 Scanner in = new Scanner(System.in);
16 System.out.println("1.複利計算");
17 System.out.println("2.單利計算");
18 System.out.println("3.本金計算");
19 System.out.println("4.求期限");
20 System.out.print("請選擇:");
21 int select = in.nextInt();
22 if(select>4||select<1){
23 System.out.println("選擇有誤!");
24 System.out.print("請重新選擇:");
25 select = in.nextInt();
26 }
27 //單、複利的輸入
28 if(select==2||select==1){
29 System.out.print("輸入本金:");
30 P = in.nextInt();
31 System.out.print("輸入利率:");
32 i = in.nextDouble();
33 System.out.print("輸入期數:");
34 n = in.nextInt();
35 }
36 else if(select==3){
37 //求本金的輸入
38 System.out.print("輸入終值:");
39 F = in.nextInt();
40 System.out.print("輸入利率:");
41 i = in.nextDouble();
42 System.out.print("輸入期數:");
43 n = in.nextInt();
44 }
45 else{
46 System.out.print("輸入本金:");
47 P = in.nextInt();
48 System.out.print("輸入利率:");
49 i = in.nextDouble();
50 System.out.print("輸入終值:");
51 F = in.nextInt();
52 }
53
54
55 jiXuan jx = new jiXuan();
56 //複利處理
57 if(select==1){
58 System.out.print("輸入年複利次數:");
59 int m = in.nextInt();
60 jx.fuLi(P, i, n, m);
61 }
62 //單利處理
63 else if(select==2){
64 jx.danLi(P, i, n);
65 }
66 else if(select==3){
67 jx.benJi(F, i, n);
68 }
69 else if(select==4){
70 jx.time(F, i, P);
71
72 }
73 }
74 }
75
76
77
78 import java.text.DecimalFormat;
79
80 public class jiXuan {
81 public void fuLi(int P,double i,int n,int m){
82 DecimalFormat df=new DecimalFormat("#.000");
83 double F = P*(Math.pow((1+i/m),n*m));
84 System.out.println("複利終值:"+df.format(F));
85
86 }
87 public void danLi(int P,double i,int n){
88 DecimalFormat df=new DecimalFormat("#.000");
89 double F =P*(1+i*n) ;
90 System.out.println("單利終值:"+df.format(F));
91
92 }
93 public void benJi(int F,double i,int n){
94 double P= F / (1 + i * n);
95 System.out.println("本金為:"+P);
96 }
97 public void time(int F,double i,double P){
98 DecimalFormat df=new DecimalFormat("#.000");
99 double n = ((F/P)-1)/i;
100 System.out.println("期限為:"+df.format(n));
101 }
102 }
