1. 程式人生 > >Java-貸款計算器小程式

Java-貸款計算器小程式

請編寫一個貸款計算器小程式,具體功能如下:使用者通過控制檯輸入貸款利率(單位為%,例如輸入5.6表示貸款利率為5.6%)、貸款金額(單位為萬元,例如輸入100表示貸款金額為100萬)、貸款年限(單位為年,例如輸入20表示貸款年限為20年)後,總還款金額(單位為萬元,保留兩位小數)和月還款金額(單位為元,保留兩位小數)就會顯示在螢幕上。

import java.util.Scanner;
public class LoanFX{
    double rate;
    double money;
    int year;
    LoanFX(double rate,double money,int year){
        this.year=year;
        this.rate=rate;
        this.money=money;
    }
    public double getSumMoney(){
        return money*(1+rate/100*year);
    }
    public double getMonthMoney(){
        return 10000*money*(1+rate/100*year)/year/12;
    }
    public static void main(String []args){
        Scanner input = new Scanner(System.in);
        LoanFX my = new LoanFX(input.nextDouble(),input.nextDouble(),input.nextInt());
        System.out.printf("總還款:%.2f萬元\n",my.getSumMoney());
        System.out.printf("月還款:%.2f元",my.getMonthMoney());
    }
}

將上述程式中的控制檯輸入改成對話方塊輸入,並將螢幕列印改成對話方塊輸出。 

執行效果:

 

import java.text.DecimalFormat;
import java.util.Scanner;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.control.*;
class Loan{
    double rate;
    double money;
    int year;
    Loan(double rate,double money,int year){
        this.year=year;
        this.rate=rate;
        this.money=money;
    }
    public double getSumMoney(){
        return money*(1+rate/100*year);
    }
    public double getMonthMoney(){
        return 10000*money*(1+rate/100*year)/(year*12);
    }
}
public class LoanFX extends Application{
    Loan my;
    TextField rate=new TextField();
    TextField money=new TextField();
    TextField year=new TextField();
    Button button=new Button("計算");
    Label sum=new Label("0.00");
    Label month=new Label("0.00");
    public void start(Stage root){
        rate.setAlignment(Pos.CENTER);
        money.setAlignment(Pos.CENTER);
        year.setAlignment(Pos.CENTER);
        GridPane pane=new GridPane();
        pane.setHgap(20);
        pane.setVgap(15);
        pane.add(new Label("貸款利率"),3,1);
        pane.add(rate,4,1,3,1);
        pane.add(new Label("%"),7,1);
        pane.add(new Label("貸款金額"),3,2);
        pane.add(money,4,2,3,1);
        pane.add(new Label("萬元"),7,2);
        pane.add(new Label("貸款年限"),3,3);
        pane.add(year,4,3,3,1);
        pane.add(new Label("年"),7,3);
        pane.add(button,4,4);
        pane.add(new Label("月還款"),3,5);
        pane.add(month,4,5);
        pane.add(new Label("元"),5,5);
        pane.add(new Label("總還款"),3,6);
        pane.add(new Label("萬元"),5,6);
        pane.add(sum,4,6);
        button.setOnMouseClicked(e->{
            my=new Loan(Double.valueOf(rate.getText()),Double.valueOf(money.getText()),Integer.valueOf(year.getText()));
            DecimalFormat df =  new DecimalFormat("#.00");
            month.setText(df.format(my.getMonthMoney()));
            sum.setText(df.format(my.getSumMoney()));
        });
        Scene scene = new Scene(pane);
        root.setScene(scene);
        root.setTitle("貸款計算器");
        root.setWidth(370);
        root.setHeight(250);
        root.setResizable(false);
        root.show();
    }

}