Java 银行利息
我想做一个程序,用原理、利率和年数计算账户的复利。我试着用对话框/程序有输出,如果让原理累积,则返回投资 我被困在计算部分,请帮忙Java 银行利息,java,double,joptionpane,banking,Java,Double,Joptionpane,Banking,我想做一个程序,用原理、利率和年数计算账户的复利。我试着用对话框/程序有输出,如果让原理累积,则返回投资 我被困在计算部分,请帮忙 package firstAssignment; import java.util.Scanner; import javax.swing.JOptionPane; public class thinkingQuestion { public static void main(String[] args) { //Banking program that
package firstAssignment;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class thinkingQuestion {
public static void main(String[] args) {
//Banking program that asks user for the amount of money they wish to invest in a
//compound interest account (principle), the interest rate (percent value) and the time frame (years).
Scanner in= new Scanner(System.in);
String principle, interestVal, years;
int newPrinciple,newYears;
double total;
principle=JOptionPane.showInputDialog("How much money would you like to invest?");
interestVal=JOptionPane.showInputDialog("What's the interest rate?");
years=JOptionPane.showInputDialog("How many years?");
//convert from String to integer
newPrinciple=Integer.parseInt(principle);
newYears=Integer.parseInt(years);
double newInterestVal=Integer.parseInt(interestVal);
total=JOptionPane.PLAIN_MESSAGE(newPrinciple*Math.pow(1+ newInterestVal, newYears), newYears);
我删除了您不需要的somo变量,我认为主要问题在于显示消息的java语法。在这里您可以看到一个教程:
我特别专注于合计部分,因为它不允许我在问题中使用利息公式。合计=newPrinciple*Math.pow((1+newInterestVal),newYears);JOptionPane.showMessageDialog(null,total+“”,“total”,JOptionPane.INFORMATION\u MESSAGE);此外,您还必须输入复利周期数,n。这里我假设n=1它显示了将字符串转换为int的错误,特别是第31行它在测试它时显示了错误,它说是将字符串转换为双精度或int行。您使用的是哪个版本的java?eclipse kepler,我的学校不允许我们使用更高版本在窗口菜单上转到preferences>java>compiler。告诉我哪一个是编译器符合性级别编译器级别是1.4
import javax.swing.JOptionPane;
public class InterestBanking {
public static void main(String[] args) {
// Banking program that asks user for the amount of money they wish to
// invest in a
// compound interest account (principle), the interest rate (percent
// value) and the time frame (years).
String principle, interestVal, years;
float newPrinciple, newYears;
principle = JOptionPane.showInputDialog("How much money would you like to invest?");
interestVal = JOptionPane.showInputDialog("What's the interest rate?");
years = JOptionPane.showInputDialog("How many years?");
// convert from String to integer
newPrinciple = Float.parseFloat(principle);
newYears = Float.parseFloat(years);
double newInterestVal = Float.parseFloat(interestVal);
//You could change your calculation here if this isn't the need formula
double interest = newPrinciple * Math.pow(1 + newInterestVal, newYears);
//you were assigning the result to a total variable. That's not neccesary
JOptionPane.showMessageDialog(null, "Interest:" + NumberFormat.getCurrencyInstance(new Locale("en", "US")).format(interest) + " In years: " + newYears);
}
}