0317复利计算3.0
生活随笔
收集整理的這篇文章主要介紹了
0317复利计算3.0
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 package kxj;
2 import java.util.Scanner;
3
4 public class Fulijisuan {
5 public static double p,i,f ;
6 public static double n;
7
8 //計算本金
9 public static void Benjin(){
10 //int n;
11 //float f,i,p;
12 Scanner scanner=new Scanner(System.in);
13 System.out.println("請輸入終值: ");
14 f=scanner.nextDouble();
15 System.out.println("請輸入年利率: ");
16 i=scanner.nextDouble();
17 System.out.println("請輸入年數: ");
18 n=scanner.nextInt();
19 p=(float) (f*1/Math.pow(1+i, n));
20 System.out.println("本金為: "+(double)(Math.round(p*100)/100.0));
21
22 }
23
24 //計算本息和
25 public static void Benxihe(){
26 double sum1,sum2;
27 Scanner scanner=new Scanner(System.in);
28 System.out.println("請輸入本金: ");
29 p=scanner.nextDouble();
30 System.out.println("請輸入年利率: ");
31 i=scanner.nextDouble();
32 System.out.println("請輸入年數: ");
33 n=scanner.nextInt();
34 sum1=(float) (p*Math.pow(1+i, n));
35 sum2=p*(1+i*n);
36 System.out.println("復利的本息和為: "+(double)(Math.round(sum1*100)/100.0));
37 System.out.println("單利的本息和為: "+(double)(Math.round(sum2*100)/100.0));
38 }
39
40 //計算年數
41 public static void Nianshu(){
42 Scanner scanner=new Scanner(System.in);
43 System.out.println("請輸入本金: ");
44 p=scanner.nextDouble();
45 System.out.println("請輸入終值: ");
46 f=scanner.nextDouble();
47 System.out.println("請輸入年利率: ");
48 i=scanner.nextDouble();
49 n=Logarithm.log(f/p,1+i);
50 n=Math.ceil(n);
51 System.out.println("需要存的年數為: "+Math.ceil(n));
52 }
53
54 //計算年利率
55 public static void Lilv(){
56 Scanner scanner=new Scanner(System.in);
57 System.out.println("請輸入本金: ");
58 p=scanner.nextDouble();
59 System.out.println("請輸入終值: ");
60 f=scanner.nextDouble();
61 System.out.println("請輸入年數: ");
62 n=scanner.nextInt();
63 i=Math.pow(f/p, 1.0/n)-1;
64 System.out.println("年報酬率為: "+(double)(Math.round(i*1000)/1000.0));
65 }
66
67 //計算本利之和連同年金投資后的總資產
68 public static void Nianjin(){
69 int k=1;
70 f=0;
71 Scanner scanner=new Scanner(System.in);
72 System.out.println("請輸入本金: ");
73 p=scanner.nextDouble();
74 System.out.println("請輸入年利率: ");
75 i=scanner.nextDouble();
76 System.out.println("請輸入年數: ");
77 n=scanner.nextInt();
78 while(k<=n){
79 p=p+f;
80 f=p*(1+i);
81 k++;
82 }
83 System.out.println("年資產總值為:"+(int)f);
84 }
85
86
87 public static void main(String[] args) {
88 int choice;
89 while(true){
90 System.out.println("\t\t|*******************|");
91 System.out.println("\t\t| 1.求本金 |");
92 System.out.println("\t\t| 2.求本息和 |");
93 System.out.println("\t\t| 3.求年數 |");
94 System.out.println("\t\t| 4.求利率 |");
95 System.out.println("\t\t| 5.求年資產總值 |");
96 System.out.println("\t\t| 6.退出 |");
97 System.out.println("\t\t|*************|");
98 Scanner scanner=new Scanner(System.in);
99 System.out.println("請輸入你的選擇(1~6): ");
100 choice=scanner.nextInt();
101 switch(choice){
102 case 1:
103 Benjin();
104 break;
105 case 2:
106 Benxihe();
107 break;
108 case 3:
109 Nianshu();
110 break;
111 case 4:
112 Lilv();
113 break;
114 case 5:
115 Nianjin();
116 break;
117 case 6:
118 System.exit(0);
119 break;
120 default:
121 {
122 System.out.println("輸入有誤!");
123 break;
124 }
125 }
126 }
127 }
128 } 1 package kxj;
2
3 public class Logarithm {
4 static public double log(double value, double base){
5 return Math.log(value)/Math.log(base);
6
7 }
8 }
?功能說明:
新增了計算期限、年利率,以及每年都將本利和連同年金投入獲得的年資產總值的功能。
轉載于:https://www.cnblogs.com/950525kxj/p/5288200.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的0317复利计算3.0的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js事件流的三个阶段
- 下一篇: linux下挂载移动硬盘