1 import java.util.Scanner; 2 3 public class text { 4 public static void main(String[] args) { 5 6 7 do { 8 Scanner scanner = new Scanner(System.in); 9 System.out.println(" "); 10 System.out.print("请选择(0为退出,1为算本息,2为算本金,3为算投资,4为算n年后资产总值):"); 11 int number1 = scanner.nextInt(); 12 13 if (number1 == 1) { 14 System.out.print("请输入本金:"); 15 double money = scanner.nextDouble(); 16 System.out.print("请输入年利率:"); 17 double rate = scanner.nextDouble(); 18 System.out.print("请输入年限:"); 19 int year = scanner.nextInt(); 20 21 System.out.print("请选择(1为复利计算,2为单利计算):"); 22 int number2 = scanner.nextInt(); 23 24 if (number2 == 1) { 25 for (int i = 1; i <= year; i++) { 26 money = (1 + rate) * money; 27 } 28 System.out.println("存入" + year + "年后的复利计算本息为:" +(double) (Math.round(money*100)/100.0)); 29 30 } 31 if (number2 == 2) { 32 money = money + money * rate * year; 33 System.out.println("存入" + year + "年后的单利计算本息为:" +(double) (Math.round(money*100)/100.0)); 34 } 35 36 37 38 } 39 40 if (number1 == 2) { 41 System.out.print("请输入本息:"); 42 double money = scanner.nextDouble(); 43 System.out.print("请输入年利率:"); 44 double rate = scanner.nextDouble(); 45 System.out.print("请输入年限:"); 46 int year = scanner.nextInt(); 47 48 System.out.print("请选择(1为复利计算,2为单利计算):"); 49 int number2 = scanner.nextInt(); 50 51 if (number2 == 1) { 52 double num = 1; 53 for (int i = 1; i <= year; i++) { 54 num = num * (1 + rate); 55 } 56 money = money * (1 / num); 57 System.out.println("复利计算需要存入的本金为:" +(double) (Math.round(money*100)/100.0)); 58 } 59 if (number2 == 2) { 60 money = money * (1 / (1 + rate * year)); 61 System.out.println("单利计算需要存入的本金为:" + (double) (Math.round(money*100)/100.0)); 62 } 63 64 } 65 if (number1 == 3) { 66 67 System.out.print("请输入投资金额:"); 68 double money1 = scanner.nextDouble(); 69 System.out.print("请输入收获金额:"); 70 double money2 = scanner.nextDouble(); 71 72 System.out.print("请选择(1为算时间,2为算利率):"); 73 int number2 = scanner.nextInt(); 74 75 if (number2 == 1) { 76 System.out.print("请输入年利率:"); 77 double rate = scanner.nextDouble(); 78 double year; 79 year = Logarithm.log(money2/money1,1+rate); 80 year = Math.ceil(year); 81 System.out.println("需要存入的年限为:" + year); 82 } else if (number2 == 2) { 83 System.out.print("请输入年限:"); 84 int year = scanner.nextInt(); 85 double rate; 86 rate = Math.pow(money2/money1, 1.0/year)-1; 87 System.out.println("所需年利率为:" + (double) (Math.round(rate*10000)/10000.0)); 88 } 89 } 90 91 if(number1 == 4){ 92 System.out.print("请输入每年的投资金额:"); 93 double money1 = scanner.nextDouble(); 94 System.out.print("请输入年利率:"); 95 double rate = scanner.nextDouble(); 96 System.out.print("请输入年限:"); 97 int year = scanner.nextInt();double money2=0; 98 for(int i=1;i<=year;i++){ 99 100 money1=money1+money2; 101 money2=money1*(1+rate); 102 103 } 104 System.out.println(year+"年后的资产总值为:" + (double) (Math.round(money2*100)/100.0)); 105 } 106 107 if (number1 == 0) { 108 System.out.println("谢谢使用!" ); 109 System.exit(0); 110 111 } 112 } while (true); 113 } 114 }
1 public class Logarithm { 2 static public double log(double value, double base){ 3 return Math.log(value)/Math.log(base); 4 5 } 6 7 }
时间: 2024-12-04 18:30:14