-1

我的代码有两个问题。
第一:我似乎无法在正确的位置添加“$”(我无法让它看起来像 10.00 美元,只有 10.00 美元)
第二:添加一个 Scanner 类最终会导致程序“运行”但没有任何反应。(如果我用一个数字设置毛,它运行良好,但不使用扫描仪类)

import java.util.Scanner;
public class Payment
{
    public static void main(String[] args) 
    { 
        Scanner Keyboard = new Scanner(System.in);
        //double gross = Keyboard.nextDouble(); will not work
        //double gross = 8000; will work
        double fed = (0.15 * gross);
        double state = (0.035 * gross);
        double soc = (0.0575 * gross);
        double med = (0.0275 * gross);
        double pen = (0.05 * gross);
        double hea = 75;
        double net = (gross - (fed + state + soc + med + pen + hea));

        System.out.println("Paycheck calculation by employee\n");
        System.out.printf("Gross Amount:%28.2f%n", gross);
        System.out.printf("Federal Tax:%29.2f%n", fed);
        System.out.printf("State Tax:%31.2f%n", state);
        System.out.printf("Social Security Tax:%21.2f%n", soc);
        System.out.printf("Medicare/Medicaid Tax:%19.2f%n", med);
        System.out.printf("Pension Plan %28.2f%n", pen);
        System.out.printf("Health Insurance %24.2f%n%n", hea);
        System.out.printf("Net Pay:%33.2f", net);
    }
}
4

2 回答 2

1

您可能想要打印出输入提示。关于货币格式,您可以使用 DecimalFormat 类。

import java.text.DecimalFormat;
import java.util.Scanner;
public class Payment
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter gross amount: ");
        double gross = keyboard.nextDouble();
        //double gross = 800; //will work
        double fed = (0.15 * gross);
        double state = (0.035 * gross);
        double soc = (0.0575 * gross);
        double med = (0.0275 * gross);
        double pen = (0.05 * gross);
        double hea = 75;
        double net = (gross - (fed + state + soc + med + pen + hea));
        DecimalFormat currency = new DecimalFormat("$0.00");
        System.out.println("Paycheck calculation by employee\n");
        System.out.printf("Gross Amount: %27s%n", currency.format(gross));
        System.out.printf("Federal Tax:%29s%n", currency.format(fed));
        System.out.printf("State Tax:%31s%n", currency.format(state));
        System.out.printf("Social Security Tax:%21s%n", currency.format(soc));
        System.out.printf("Medicare/Medicaid Tax:%19s%n", currency.format(med));
        System.out.printf("Pension Plan %28s%n", currency.format(pen));
        System.out.printf("Health Insurance %24s%n%n", currency.format(hea));
        System.out.printf("Net Pay:%33s", currency.format(net));
        keyboard.close();
    }
}
于 2014-02-05T01:16:05.430 回答
0

回答你的第一个问题,你可以这样:

System.out.printf("Gross Amount %28c%.2f%n", '$',  gross);

你的第二个问题,我认为你的问题是Locale. 根据您的 ,在这种情况下, Localea 的输入格式Double可能会有所不同。你可以这样做:

keyboard.useLocale(Locale.US);

这样,a 的输入Double将是由 a.与小数部分分隔的整数部分。8000并且5.5Double输入的有效示例。

于 2014-02-05T01:19:09.407 回答