0320复利计算4.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          Scanner scanner=new Scanner(System.in);
 70           System.out.println("请输入每年定投资金: ");
 71           p=scanner.nextDouble(); 
 72           System.out.println("请输入年利率: ");
 73           i=scanner.nextDouble();
 74           System.out.println("请输入年数: ");
 75           n=scanner.nextInt();
 76           f=p*(1+i)*(Math.pow(1+i,n)-1)/i;
 77           System.out.println("年资产总值为:"+(double)(Math.round(f*100)/100.0));     
 78     }
 79     
 80     //计算每月等额本息还款
 81     public static void BenxiHuankuan(){
 82         double f,i,p;
 83         int n;
 84          Scanner scanner=new Scanner(System.in);
 85           System.out.println("请输入贷款金额: ");
 86           f=scanner.nextDouble();
 87           System.out.println("请输入年利率: ");
 88           i=scanner.nextDouble();
 89           System.out.println("请输入贷款年数: ");
 90           n=scanner.nextInt();
 91           i=i/12;
 92           n=n*12;
 93           p=f*i*Math.pow(1+i, n)/(Math.pow(1+i, n)-1);
 94          System.out.println("每月等额本息还款为:"+(double)(Math.round(p*10000)/10000.0));
 95         
 96     }
 97  
 98      public static void main(String[] args) {
 99          int choice;
100          while(true){
101          System.out.println("\t\t|***********************|");
102          System.out.println("\t\t|  1. 求       本      金  \t|");
103          System.out.println("\t\t|  2. 求   本   息   和 \t|");
104          System.out.println("\t\t|  3. 求      年       数 \t|");
105         System.out.println("\t\t|  4. 求      利       率 \t|");
106         System.out.println("\t\t|  5. 求年资产总值\t|");
107         System.out.println("\t\t|  6. 求等额本息还款\t|");    
108          System.out.println("\t\t|  7. 退        出          \t|");
109          System.out.println("\t\t|***********************|");
110          Scanner scanner=new Scanner(System.in);
111          System.out.println("\n请输入你的选择(1~7):  ");
112          choice=scanner.nextInt();
113          switch(choice){
114          case 1:
115              Benjin();
116              break;
117          case 2:
118              Benxihe();
119              break;
120          case 3:
121              Nianshu();
122              break;
123          case 4:
124              Lilv();
125              break;
126          case 5:
127              Nianjin();
128              break;
129          case 6:
130              BenxiHuankuan();
131              break;
132          case 7:
133              System.out.println("Thanks for using!");
134              System.exit(0);
135              break;
136              default:
137              {
138                  System.out.println("输入有误!");
139                  break;
140              }
141          }
142              }        
143          }
144      }

 功能:增加了复利条件下等额本息还款的功能。

posted on 2016-03-20 17:29  10柯晓君  阅读(184)  评论(0编辑  收藏  举报

导航