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("		|*******************|");
 91          System.out.println("		|  1.求本金                         |");
 92          System.out.println("		|  2.求本息和                    |");
 93          System.out.println("		|  3.求年数                         |");
 94         System.out.println("		|  4.求利率                         |");
 95         System.out.println("		|  5.求年资产总值          |");
 96          System.out.println("		|  6.退出              |");
 97          System.out.println("		|*************|");
 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