十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
先定义个sum:double sum=0;
府谷ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
然后在for循环中加入这代码 就好了:sum=repayInterest+sum;
money(1+rate)^year - money
是money * (1+rate)^year - money,和普通的数学顺序一样,先计算级别高的,括号内的1+rate、再乘方year、再乘 money,最后减money。
转成java的计算式,也是按顺序的,乘方的地方就是按JAVA的函数pow的要求,写进参数。
money*Math.pow((1+rate),year)-money;
这个很简单,只是我对这些计算规则不熟悉
import java.util.ListIterator;
import java.util.Stack;
public class CalStr {
private String src;
/**
* constructor
*
* @param srcthe string(expression) to calculate
*/
public CalStr(String src) {
this.src = src;
}
/**
* calculate to get the result
*
* @return(double)result
*/
public double getResult() {
String postfix = getPostfix();
Stack stk = new Stack();
// System.out.println(postfix);
String parts[] = postfix.split(" +");
double result = 0;
for (int i = 0; i parts.length; i++) {
char tmp = parts[i].charAt(0);
if (!isOperator(tmp)) {
stk.push(parts[i]);
} else {
double a = Double.parseDouble((String) stk.pop());
double b = Double.parseDouble((String) stk.pop());
// b is followed by a in the orignal expression
result = calculate(b, a, tmp);
stk.push(String.valueOf(result));
}
}
return result;
}
/**
* test if the character is an operator,such +,-,*,/
*
* @param opthe character to test
* @returntrue if op is an operator otherwise false
*/
private boolean isOperator(char op) {
return (op == '+' || op == '-' || op == '*' || op == '/');
}
/**
* calculate an expression such (a op b)
*
* @param anumber 1
* @param bnumber 2
* @param opthe operator
* @return(double)(a op b)
*/
public double calculate(double a, double b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return -1;
}
/**
* convert the suffix to postfix
*
* @returnthe postfix as a string
*/
private String getPostfix() {
Stack stk = new Stack();
String postfix = new String();
char op;
int i = 0;
while (i src.length()) {
if (Character.isDigit(src.charAt(i)) || src.charAt(i) == '.') {
postfix += " ";
do {
postfix += src.charAt(i++);
} while ((i src.length())
(Character.isDigit(src.charAt(i))));
postfix += " ";
}
else {
switch (op = src.charAt(i++)) {
case '(':
stk.push("(");
break;
case ')':
while (stk.peek() != "(") {
String tmp = (String) stk.pop();
postfix += tmp;
if (tmp.length() == 1 isOperator(tmp.charAt(0)))
postfix += " ";
}
stk.pop();
postfix += " ";
break;
case '+':
case '-':
while ((!stk.empty()) (stk.peek() != "(")) {
postfix += stk.pop() + " ";
}
stk.push(String.valueOf(new Character(op)));
break;
case '*':
case '/':
while ((!stk.empty())
((stk.peek() == "*") || (stk.peek() == "/"))) {
postfix += stk.pop() + " ";
}
stk.push(String.valueOf(new Character(op)));
break;
}
}
}
ListIterator it = stk.listIterator(stk.size());
while (it.hasPrevious())
postfix += it.previous() + " ";
return postfix.trim().replaceAll(" +\\.", ".");
}
/**
* main function
*
* @param args
*/
public static void main(String args[]) {
System.out.println(new CalStr("((1.5+6.000)*9+9.36)*(8+9-8*8+8*7)")
.getResult());
}
}
new CalStr( 写上你的计算公式 );
package com.nsu.java.base;
import java.util.Scanner;
public class homework {
public static void main(String[] args) {
double a=0.0115;
double b;
double d;
double c,f;
System.out.println("请输入您的存款金额:");
Scanner it=new Scanner(System.in);
d=it.nextDouble();
System.out.println("请输入您的存款期限:");
b=it.nextDouble();
c=d-d+d*a;
f=b*c;
System.out.println("您的预期收入为:"+f);
}
}
建议把上面的包名改一下。
/**
需求:本金1万元人民币,以一年期整存整取的形式储蓄在银行,一年期利率为2.79%,n年后连本带息共计多少钱。
*/
class Calculate
{
public double calcuTotal(double prin,double rate,int n) //参数分别是本金、利率,存放期
{
double inte=0.00; //利息
double sum=prin;
for(int i=0;in;i++)
{
inte=inte+prin*rate; //一年期整存整存的利息算法
}
sum+=inte;
return sum;
}
}
class ParamSet
{
public static void main(String[] args)
{
//设置你要的参数
double prin=10000.00;
double rate=0.0279;
int n=10;
//存多少年
Calculate c = new Calculate();
double sum=c.calcuTotal(prin,rate,n);
System.out.println("本金:"+prin+" 元 \n存款利率:"+rate+'\n'+n+"年后连本带息共为:"+sum+"元");
}
}
简单些了个,如果没理解错的话,应该可以满足要求:
public class Benxi{
private double benxi;//本息
private double lilu;//年利率
//计算本息
private double resBenxi(double money,int year){
benxi=money+money*getLilu(year)*year;
return benxi;
}
//选择利率
private double getLilu(int year){
switch(year){
case 1:
lilu=2.25/100;
break;
case 2:
lilu=2.7/100;
break;
case 3:
lilu=3.24/100;
break;
case 5:
lilu=3.6/100;
break;
}
return lilu;
}
public static void main(String[] args){
Benxi bx=new Benxi();
System.out.println("10000元存一年的本息为:"+bx.resBenxi(10000,1));
System.out.println("10000元存两年的本息为:"+bx.resBenxi(10000,2));
System.out.println("10000元存三年的本息为:"+bx.resBenxi(10000,3));
System.out.println("10000元存五年的本息为:"+bx.resBenxi(10000,5));
}
}