码迷,mamicode.com
首页 > 其他好文 > 详细

0406复利计算5.0

时间:2016-04-06 23:27:59      阅读:287      评论:0      收藏: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      public static int a=0;
  8      
  9      static double A[]  = new double[10];
 10         static double B[]  = new double[10];
 11         static double C[]  = new double[10];
 12         static double D[]  = new double[10];
 13      
 14      //计算本金
 15     public static void Benjin(){
 16         //int n;
 17         //float f,i,p;
 18         boolean flag;
 19          Scanner scanner=new Scanner(System.in);
 20           System.out.println("请输入终值: ");
 21           f=scanner.nextDouble();
 22           System.out.println("请输入年利率: ");
 23           i=scanner.nextDouble();
 24           System.out.println("请输入年数: ");
 25           n=scanner.nextInt();
 26           if(n>=0)
 27           {
 28               if(i>=0&&i<=1)
 29               {
 30                  flag=true;
 31                  p=(float) (f*1/Math.pow(1+i, n));
 32               }
 33               else
 34               {
 35                   System.out.println("输入的年利率有错!");
 36                   flag=false;
 37                   
 38               }
 39           }
 40           else
 41           {
 42               System.out.println("输入的年数有错!");
 43               flag=false;
 44               
 45           }
 46           if(flag)
 47               System.out.println("本金为: "+(double)(Math.round(p*100)/100.0));
 48     
 49     }
 50     
 51     //计算本息和
 52     public static void Benxihe(){
 53          double sum1=0,sum2=0;
 54          boolean flag;
 55           Scanner scanner=new Scanner(System.in);
 56           System.out.println("请输入本金: ");
 57           p=scanner.nextDouble();
 58           System.out.println("请输入年利率: ");
 59           i=scanner.nextDouble();
 60           System.out.println("请输入年数: ");
 61           n=scanner.nextInt();
 62          if(n>=0)
 63           {
 64               if(i>=0&&i<=1)
 65               {
 66                   sum1=(float) (p*Math.pow(1+i, n));
 67                    sum2=p*(1+i*n);
 68                    flag=true;
 69                    
 70               }
 71               else
 72               {
 73                   System.out.println("输入的年利率有错!");
 74                   flag=false;
 75                   
 76               }
 77           }
 78           else
 79           {
 80               System.out.println("输入的年数有错!");
 81               flag=false;
 82               
 83           }
 84           if(flag)
 85           {
 86              System.out.println("复利的本息和为: "+(double)(Math.round(sum1*100)/100.0));
 87              System.out.println("单利的本息和为: "+(double)(Math.round(sum2*100)/100.0));
 88           }
 89     }
 90     
 91     //计算年数
 92     public static void Nianshu(){
 93         boolean flag;
 94           Scanner scanner=new Scanner(System.in);
 95           System.out.println("请输入本金: ");
 96           p=scanner.nextDouble();
 97           System.out.println("请输入终值: ");
 98           f=scanner.nextDouble();
 99           System.out.println("请输入年利率: ");
100           i=scanner.nextDouble();
101          if(i>=0&&i<=1)
102          {
103              n=Logarithm.log(f/p,1+i); 
104              flag=true;
105          }
106          else
107          {
108             System.out.println("输入的年利率有错!"); 
109             flag=false;
110 
111          }
112          if(flag)
113           System.out.println("需要存的年数为: "+Math.ceil(n));     
114     }
115     
116     //计算年利率
117     public static void Lilv(){
118         boolean flag;
119          Scanner scanner=new Scanner(System.in);
120           System.out.println("请输入本金: ");
121           p=scanner.nextDouble();
122           System.out.println("请输入终值: ");
123           f=scanner.nextDouble();
124           System.out.println("请输入年数: ");
125           n=scanner.nextInt();
126          if(n>=0)
127           {
128               i=Math.pow(f/p, 1.0/n)-1;
129               flag=true;
130           }
131           else
132           {
133               System.out.println("输入的年数有错!");
134               flag=false;
135               
136           }
137           if(flag)
138           System.out.println("年报酬率为: "+(double)(Math.round(i*1000)/1000.0));
139     }
140     
141     //计算本利之和连同年金投资后的总资产
142     public static void Nianjin(){
143         boolean flag;
144          Scanner scanner=new Scanner(System.in);
145           System.out.println("请输入每年定投资金: ");
146           p=scanner.nextDouble(); 
147           System.out.println("请输入年利率: ");
148           i=scanner.nextDouble();
149           System.out.println("请输入年数: ");
150           n=scanner.nextInt();
151          if(n>=0)
152           {
153               if(i>=0&&i<=1)
154               {
155                    f=p*(1+i)*(Math.pow(1+i,n)-1)/i;
156                    flag=true;
157                    
158               }
159               else
160               {
161                   System.out.println("输入的年利率有错!");
162                   flag=false;
163                   
164               }
165           }
166           else
167           {
168               System.out.println("输入的年数有错!");
169               flag=false;
170               
171           }
172           if(flag)
173           System.out.println("年资产总值为:"+(double)(Math.round(f*100)/100.0));     
174     }
175     
176     //计算每月等额本息还款
177     public static void BenxiHuankuan(){
178         double f,i,p = 0;
179         int n;
180         boolean flag;
181          Scanner scanner=new Scanner(System.in);
182           System.out.println("请输入贷款金额: ");
183           f=scanner.nextDouble();
184           System.out.println("请输入年利率: ");
185           i=scanner.nextDouble();
186           System.out.println("请输入贷款年数: ");
187           n=scanner.nextInt();
188          if(n>=0)
189           {
190               if(i>=0&&i<=1)
191               {
192                    i=i/12;
193                    n=n*12;
194                    p=f*i*Math.pow(1+i, n)/(Math.pow(1+i, n)-1);
195                    flag=true;
196                    
197               }
198               else
199               {
200                   System.out.println("输入的年利率有错!");
201                   flag=false;
202                   
203               }
204           }
205           else
206           {
207               System.out.println("输入的年数有错!");
208               flag=false;
209               
210           }
211           if(flag)
212          System.out.println("每月等额本息还款为:"+(double)(Math.round(p*10000)/10000.0));
213         
214     }
215  
216      public static void main(String[] args) {
217          int choice;
218          while(true){
219          System.out.println("\t\t|***********************|");
220          System.out.println("\t\t|  1. 求       本      金  \t|");
221          System.out.println("\t\t|  2. 求   本   息   和 \t|");
222          System.out.println("\t\t|  3. 求      年       数 \t|");
223         System.out.println("\t\t|  4. 求      利       率 \t|");
224         System.out.println("\t\t|  5. 求年资产总值\t|");
225         System.out.println("\t\t|  6. 求等额本息还款\t|");    
226          System.out.println("\t\t|  7. 投   资   运    算     \t|");
227          System.out.println("\t\t|  0. 退        出          \t|");
228          System.out.println("\t\t|***********************|");
229          Scanner scanner=new Scanner(System.in);
230          System.out.println("\n请输入你的选择(0~7):  ");
231          choice=scanner.nextInt();
232          switch(choice){
233          case 1:
234              Benjin();
235              break;
236          case 2:
237              Benxihe();
238              break;
239          case 3:
240              Nianshu();
241              break;
242          case 4:
243              Lilv();
244              break;
245          case 5:
246              Nianjin();
247              break;
248          case 6:
249              BenxiHuankuan();
250              break;
251          case 7:
252              TouziYunsuan();
253          
254              a++;
255              break;
256          case 0:
257              System.out.println("Thanks for using!");
258              System.exit(0);
259              break;
260          
261              
262          default:
263              {
264                  System.out.println("输入有误!");
265                  break;
266              }
267          }
268              }        
269          }
270 
271     private static void TouziYunsuan() {
272         boolean flag;
273         int number;
274         
275           Scanner scanner=new Scanner(System.in);
276           System.out.println("请输入本金: ");
277           p=scanner.nextDouble();
278           System.out.println("请输入年利率: ");
279           i=scanner.nextDouble();
280           System.out.println("请输入年限: ");
281         n = scanner.nextInt();
282         for (int t = 1; t <= n; t++) {
283             f = p * (1 + i * t);
284 
285             System.out.println("第" + t + "年\t  " + (double)(Math.round(f*100)/100.0) + "");
286         }
287         A[a]=p;
288         B[a]=i;
289         C[a]=n;
290         D[a]=f;
291         
292         System.out.println("是否查看之前记录(1为是): ");
293         number = scanner.nextInt();
294         if(number==1){
295             System.out.print("\t"+"本金"+"\t"+"年利率"+"\t"+"年限"+"\t"+"终值"+"\n");
296             for(int b=0;b<=a;b++)
297             {
298                 System.out.print(b+1+"\t"+A[b]+"\t"+B[b]+"\t"+C[b]+"\t"+D[b]+"\n");
299 
300             }
301                 
302         }
303         else{
304             System.out.print("谢谢");
305             
306         }    
307     }
308      }
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 }

技术分享

技术分享

0406复利计算5.0

标签:

原文地址:http://www.cnblogs.com/xjy-gg/p/5361518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!