Java对话框形式的输入


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);
      
	}

}

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

你可能感兴趣的:(Java基础学习)