代码如下:
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 }