0

我试图在我的代码决赛中做几个双打。我尝试了一些方法,例如“public static final”,只是“final”等。

import java.util.Scanner;
import java.text.DecimalFormat;

public class Pay_stub {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("#.00");

        double fedinctax = .15;
        double statetax = .035;
        double socsectax = .0575;
        double meditax = .0275;
        double penplan = .05;
        double healthins = 75;

        double hoursworked;
        double hourlyrate;

        double netpay;
        double grosspay;

        String firstname, lastname;

        System.out.println("Enter employee first name: ");
        firstname = input.next();

        System.out.println("Enter employee last name: ");
        lastname = input.next();

        System.out.println("Enter hours worked: ");
        hoursworked = input.nextDouble();

        System.out.println("Enter hourly rate: ");
        hourlyrate = input.nextDouble();

        grosspay = hoursworked * hourlyrate;

        double fit = grosspay * fedinctax;
        double st = grosspay * statetax;
        double sst = grosspay * socsectax;
        double mmt = grosspay * meditax;
        double pp = grosspay * penplan;
        double total = fit + st + sst + mmt + pp + healthins;
        double net = grosspay - total;

        System.out.println(firstname + lastname + "'s monthly paycheck is: ");
        System.out.println("Gross Pay: " + (df.format(grosspay)));
        System.out.println("Federal Income Tax: " + fit);
        System.out.println("State Tax: " + st);
        System.out.println("Social Security Tax: " + sst);
        System.out.println("Medicare/Medicaid Tax: " + mmt);
        System.out.println("Pension Plan: " + pp);
        System.out.println("Health Insurance: " + healthins);
        System.out.println("Net Pay: " + (df.format(net)));
    }
}

这些是我想做的最终结果:

    double fedinctax = .15;
    double statetax = .035;
    double socsectax = .0575;
    double meditax = .0275;
    double penplan = .05;
    double healthins = 75;

我只是不确定我应该将最后几行放在代码中的哪个位置,或者它们应该如何编写

4

2 回答 2

2
final double fedinctax = .15;

顺便说一句,而不是使用

final double fedinctax = .15;
final double statetax = .035;
final double socsectax = .0575;
...

你可以做

final double fedinctax = .15,
             statetax = .035,
             socsectax = .0575,
             ...

或者(缩进无关紧要)

final double fedinctax = .15, statetax = .035, socsectax = .0575, ...

所以你不必一遍又一遍地重新输入“ final double”。(同样的事情也适用于 normal doubles,或任何其他类型)

此外,最终字段的命名约定是ALL_CAPS,例如:final double STATE_TAX

于 2013-09-18T23:47:06.193 回答
0

通常,如果您有一个在运行时已知的常量列表,那么首选的做法是创建一个枚举。您可以将数据与枚举中的枚举标签相关联。

这比使常量数组静态和最终的模式更可取。枚举是类型安全的,并且为您提供实例控制,而静态最终常量仅具有有限的类型安全性。

您的常量有些异质(它们似乎并非都具有相同的基本类型),但这里有一个名为“预扣”的枚举示例:

enum Withholding {
    FEDERAL_INCOME (0.15),
    STATE_INCOME (0.035),
    SOCIAL_SECURITY (0.0575),
    MEDICARE (0.0275),
    PERSONAL_RETIREMENT (0.05);

    private final double rate;

    Withholding (double r) {
        this.rate = r;
    }

    getRate() { return this.rate; }
}

例如,要访问税前收入的预扣税率,您可以在代码中使用以下代码:

   double fed_withholding = gross_salary * Withholding.FEDERAL_INCOME.getRate();

与常量列表相比,使用枚举有很多优点。当您在编译时知道这样的列表时,您会发现使用枚举声明而不是声明的常量模式更容易维护、更易读和更健壮。

于 2013-09-18T23:48:48.090 回答