银行利息 Q
Posted
技术标签:
【中文标题】银行利息 Q【英文标题】:Banking interest Q 【发布时间】:2017-10-19 14:54:26 【问题描述】:我正在尝试制作一个程序,用本金、利率和年数计算帐户的复利。我试图用对话框来做这件事/如果原则留给积累,程序会输出投资回报。
我卡在计算部分,请帮忙
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);
【问题讨论】:
我特别卡在总计部分,因为它不允许我使用兴趣公式来解决问题。 total= newPrinciple*Math.pow((1+newInterestVal), newYears); JOptionPane.showMessageDialog (null, total+"", "Total", JOptionPane.INFORMATION_MESSAGE);您还必须输入复利期数 n。这里我假设 n=1 它显示将字符串转换为 int 的错误,尤其是第 31 行 【参考方案1】:我删除了一些你不需要的变量,我认为主要问题在于显示消息的 java 语法。在这里你可以看到一个教程:
https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
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);
【讨论】:
它在测试时显示错误,它说它正在将字符串转换为双行或整数行。 你用的是哪个版本的java? eclipse kepler,我学校不让我们用高版本 在窗口菜单上转到首选项 > java > 编译器。告诉我哪个是编译器合规级别 编译器级别为1.4以上是关于银行利息 Q的主要内容,如果未能解决你的问题,请参考以下文章