Java语言程序设计(六)对话框应用实例及随机数的产生

 我们上一篇文章说到输入对话框,输入对话框返回的是一个字符串,如果说我们输入的数字值123,那么返回的会是'123',我们必须要把字符串转化为数字值以得到数字型的输入,要把一个字符串转化成为int型值,使用Interger类中的parseInt方法。

      int intValue = Integer.parseInt(intString);这里的intstring是一个数值字符串。同样的,如果要将一个字符串转换为一个double型的值,使用Double类中的parseInt方法。

      double doubleValue = Double.parseInt(doubleString);这里的doubleString是一个数值字符串,例如:123.45.Integer类和Double类都包含在java.lang中,因此他们都是自动导入的。

      我们下面举个例子,来读取并使用对话框,我们要从对话框中读取年利率,年数,贷款总额,然后显示月支付额和总支付额。

      首先建立数学模型,已知贷款额A,贷款年限B,年利率C,求总贷款额和月供X。由已知条件,可求得B年内总共需要还款Y=A*(1+C)^B,而B年内一共有B*12个月,因此月供X=Y/(B*12),程序清单如下。     

import javax.swing.JOptionPane;

/**
 *
 * @author mjd
 */
public class ComputeLoanUsingInputDialog {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String annualInterestRateString = JOptionPane.showInputDialog("Enter yearly interest rate,\nfor example 8.25:");
        double annualInterestRate = Double.parseDouble(annualInterestRateString);
        double monthlyInterestRate = annualInterestRate/12/100;
        String numberOfYearsString = JOptionPane.showInputDialog("Enter number of years,\nfor example 5:");
        int numberOfYears = Integer.parseInt(numberOfYearsString);
        String loanString = JOptionPane.showInputDialog("Enter loan amount,\nfor example 200014.58");
        double loanAmount = Double.parseDouble(loanString);
        double totalPayment = loanAmount*Math.pow(1+annualInterestRate/100, numberOfYears);
        double monthlyPayment = totalPayment/(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);
    }
    
}

图片

      点击运行后,会显示由对话框输入 ,最后由对话框输出。

图片

      第一个显示的对话框为输入的年利率,输入double类型的数据后点击确定。

图片

        第二个显示的对话框为输入的年限,输入int类型的数据后点击确定。

图片

      第三个显示的对话框为输入的贷款总额,输入int类型或者double类型都可以,输入完成后点击确定。

图片

      最后会以对话框的形式输出月支付额和总支付额,程序完成。

      产生随机数

      首先我们介绍一下boolean数据类型(布尔类型),我们在编程中该如何比较两个值呢,例如说一个半径是大于0,小于0还是等于0呢,Java提供六种比较运算符,用于两个值的比较,<,<=,>,>=,==,!=,比较的结果是一个布尔值,true或者false,例如:

       double radius = 1;

       System.out.println(radius>0);

       这样的输出会显示true,具有布尔值的变量称为布尔变量,boolean型变量只可能是true或者false。

      我们用一个例子来验证,让程序随机产生整数,产生随机数的方法有很多种,我们现在采用其中的一种,另外几种我们在下面的文章中介绍,现在我们使用System.currentTimeMillis()%10产生第一个整数,使用System.currentTimeMillis()*7%10产生第二个整数,最后使用布尔表达式来判断答案是否正确,程序清单如下。

import java.util.Scanner;

public class Random {

    public static void main(String[] args) {
        // TODO code application logic here
        int number1 = (int)(System.currentTimeMillis()%10);
        int number2 = (int)(System.currentTimeMillis()*7%10);
        Scanner input = new Scanner(System.in);
        System.out.print("What is"+number1+"+"+number2+"?");
        int answer = input.nextInt();
        System.out.println(number1+"+"+number2+"="+answer+"is"+(number1+number2==answer));        
     }
}


图片

   

      在最下方输入框输入答案,会有两种显示结果。

图片

图片

      一种输出为true,另一种输出为false,这就是使用布尔值进行输出的案例,希望我们共同学习,一起进步,谢谢。


猜你喜欢

转载自blog.51cto.com/15064656/2602782
今日推荐