银行利息Q.

时间:2017-05-19 12:52:58

标签: 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 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);

1 个答案:

答案 0 :(得分:0)

我删除了您不需要的somo变量,我认为主要问题是show message的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);
    }
}