在JOptionPane.showMessageDialog中格式化小数点

时间:2017-08-24 03:30:51

标签: java joptionpane

我的代码是一个不断发展的过程,最终的游戏是使用JOptionPane实现我之前迭代的UI。我使用String.format(“Text goes here%。2f”,变量)成功地将输出格式化为2位小数但是当我尝试使用JOptionPane将此方法转移到我的代码迭代时,它会使程序崩溃。

这是我的代码

import javax.swing.JOptionPane;

public class Ass1d2
{
    public static void main(String [] args)
    {
        final int N = 7;
        String taxPayerName;
        int taxPayerIncome = 0;
        double maxTax = 0.0;
        String maxTaxName = "";

        JOptionPane.showMessageDialog(null, "Welcome to use Tax Computation System");

        for(int i = 0; i < N; i++)
        {

            taxPayerName = (String)JOptionPane.showInputDialog(null, "Enter tax payers name");

            taxPayerIncome =  Integer.parseInt(JOptionPane.showInputDialog(null, "Enter income for the tax payer"));

            double tax = computeTax(taxPayerIncome);

            if (taxPayerIncome > maxTax){
                maxTax = tax;
                maxTaxName = taxPayerName;
            }
            JOptionPane.showMessageDialog(null, "The tax that " + taxPayerName + " owes is $" + tax));
        }
        JOptionPane.showMessageDialog(null, "The maximum tax is $" + maxTax) + " paid by " + maxTaxName);
    }

    private static double computeTax(int taxPayerIncome)
    {
        double tax = 0.0;

        if (taxPayerIncome < 18200)
        tax = 0;

        else if (taxPayerIncome < 37000)
        tax = (taxPayerIncome - 18200) * 0.19;

        else if (taxPayerIncome < 87000)
        tax = 3572 + (taxPayerIncome - 37000) * 0.325;

        else if (taxPayerIncome < 180000)
        tax = 19822 + (taxPayerIncome - 87000) * 0.37;

        else
        tax = 54232 + (taxPayerIncome - 180000) * 0.47;

        return tax;
    }
}

如何格式化两个showMessageDialog以产生两个十进制浮点结果?过去一小时我一直在搜索导游,而且不是点击。非常令人沮丧的是,这是最后的障碍。感谢。

1 个答案:

答案 0 :(得分:0)

好吧,我必须在其他尝试中做错了,因为我现在正在努力。尴尬。附带有工作浮点的更新代码,以供将来参考。

var Image = require("parse-image");

/*
  Original: https://github.com/ParsePlatform/Anyimg/blob/master/parse/cloud/resize-image-key.js
  Resizes an image from one Parse Object key containing
  a Parse File to a file object at a new key with a target width.
  If the image is smaller than the target width, then it is simply
  copied unaltered.

  object: Parse Object
  url: URL of the Parse File
  toKey: Key to contain the target Parse File
  width: Target width
  crop: Center crop the square
*/
module.exports = function(options) {
    var format, originalHeight, originalWidth, newHeight, newWidth;

    // First get the image data
    return Parse.Cloud.httpRequest({
        //url: options.object.get(options.fromKey).url()
        url: options.url
    }).then(function(response) {
        var image = new Image();
        return image.setData(response.buffer);
    }).then(function(image) {
        // set some metadata that will be on the object
        format = image.format();
        originalHeight = image.height();
        originalWidth = image.width();

        if (image.width() <= options.width) {
            // No need to resize
            return new Parse.Promise.as(image);
        } else {
            var newWidth = options.width;
            var newHeight = options.width * image.height() / image.width();

            // If we're cropping to a square, then we need to adjust height and
            // width so that the greater length of the two fits the square
            if (options.crop && (newWidth > newHeight)) {
                var newHeight = options.width;
                var newWidth = newHeight * image.width() / image.height();
            }

            // resize down to normal width size
            return image.scale({
                width: newWidth,
                height: newHeight
            });
        }
    }).then(function(image) {
        if (options.crop) {
            var left = 0;
            var top = 0;

            // Center crop
            if (image.width() > image.height()) {
                var left = (image.width() - image.height()) / 2;
            } else {
                var top = (image.height() - image.width()) / 2;
            }

            return image.crop({
                left: left,
                top: top,
                width: options.width,
                height: options.width
            });
        } else {
            return Parse.Promise.as(image);
        }
    }).then(function(image) {
        newHeight = image.height();
        newWidth = image.width();
        // Get the image data in a Buffer.
        return image.data();
    }).then(function(buffer) {
        // Save the image into a new file.
        var base64 = buffer.toString("base64");
        //var scaled = new Parse.File("thumbnail_" + options.object.get("name") + "." + format, {
        var scaled = new Parse.File("thumbnail." + format, {
            base64: base64
        });
        return scaled.save();
    }).then(function(image) {
        // Set metadata on the image object
        options.object.set(options.toKey, image);
        //return options.object.save();
        options.object.set("thumbnail_width", newWidth);
        options.object.set("thumbnail_height", newHeight);
    });
};