我的代码存在两个问题
第一:我似乎无法在正确的位置添加“$”(我不能让它看起来像$ 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);
}
}
答案 0 :(得分: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();
}
}
答案 1 :(得分:0)
回答你的第一个问题,你可以这样:
System.out.printf("Gross Amount %28c%.2f%n", '$', gross);
你的第二个问题,我认为你的问题是Locale
。在这种情况下,Locale
的输入格式可能会有所不同,具体取决于您的Double
。你可以这样做:
keyboard.useLocale(Locale.US);
这样,Double
的输入将是小数部分中由.
分隔的整数部分。 8000
和5.5
是Double
输入的有效示例。