Java对话框形式的输入

Java基础学习 专栏收录该内容
42 篇文章 0 订阅

import javax.swing.JOptionPane;               //导入javax.swing包中的JOptionPane类,可以获取输入,类似Scanner

public class ComputeLoanUsingDialog {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
      String annualInterestRateString = JOptionPane.showInputDialog(
    		  "Enter yearly interest rate, for example 8.25: ");  //showinputdialog显示对话框,返回值是以一个字符串变量,赋给annualInterestRateString
      
      double annualInterestRate = Double.parseDouble(annualInterestRateString);  //将字符串类型转换为double类型
      
      double monthlyInterestRate = annualInterestRate/1200;
      
      String numberOfYearsString = JOptionPane.showInputDialog(
    		  "Enter number of years as an integer,\nfor example 5:");
      
      int numberOfYears = Integer.parseInt(numberOfYearsString);
      String loanString = JOptionPane.showInputDialog(
    		  "Enter loan amount, for exmaple 1200000.95:");
      
      double loanAmount = Double.parseDouble(loanString);
      
      double monthlyPayment = loanAmount * monthlyInterestRate / ( 1 - 1 / Math.pow ( 1 + monthlyInterestRate , numberOfYears * 12 ) );
      
      double totalPayment = monthlyPayment*numberOfYears*12;
      
      monthlyPayment=(int)(monthlyPayment*100)/100.0;
      totalPayment = (int)(totalPayment * 100) / 100.0;
      
      String output="The monthly payment is " + monthlyPayment + "\nThe total payment is "+totalPayment;
     
      JOptionPane.showMessageDialog(null,output);
      
	}

}

要注意字符串类型向浮点型的转化以及类的使用写法

  • 1
    点赞
  • 0
    评论
  • 9
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值