当我运行我的程序时,年度销售示例以$结尾,而不是以$开头,只有1位小数。此外,我无法弄清楚必须在数字中添加逗号。我是Java的初学者,所以我不是很熟练。
package salesman1;
/*
* Annual Compensation Program
* Created by: Big Werd
*This program calculates the total compensation for a sales person based on the
*amount of the yearly fixed income and the annual commission that was earned.
*/
public class Salesman1 {
// fixed salary variable
double fixedSalary;
// amount of the annual sales for the salesman
double annualSales;
//commission that was earned
double commission;
//The current sales goal
double target;
public Salesman1(double annualSales){
this.annualSales=annualSales;
target=120000;
commission=0;
if(annualSales>target*0.8){
if(annualSales<target)commission=0.15*annualSales;//15% Commission is earned annually
else commission=0.15*1.25*annualSales;//The acceleration factor is 1.25
}
fixedSalary=35000;// set fixed salary is $35,000
}
public double getTotalAnnualCompensation(){// Fixed salary added to the commission earned for a calculation of the total compensation.
return fixedSalary+commission;
}
}
package salesman1;
import java.util.Scanner;
/*
* Annual Compensation Program
* Created by: James Werderman
*This program calculates the total compensation for a sales person based on the
*amount of the yearly fixed income and the annual commission that was earned.
*/
public class Calculator {
public static void main(String args[]){
double annualSales;
Salesman1 person;
Scanner input=new Scanner(System.in);
System.out.print("What were the total annual sales?: ");// Requests the annual sales amount
annualSales=input.nextDouble();
person=new Salesman1(annualSales);// creates a salesperson
System.out.println(" The total annual compensation: "+String.format("%.2f", person.getTotalAnnualCompensation())+"$");
System.out.println(" Total Sales Amount Total Compensation Amount");
annualSales=annualSales+5000;
for(int i=0;i<11;i++){
person=new Salesman1(annualSales);
System.out.println(annualSales+"$ "+String.format("%.2f", person.getTotalAnnualCompensation())+"$");
annualSales+=5000;
}
}
}
答案 0 :(得分:2)
首先,您使用$
System.out.println(annualSales + "$
这意味着将首先打印annualSales
值,然后打印$
符号,切换它们
System.out.println("$" + annualSales + "
你也可以做点像......
System.out.printf(" $%17.2f | $%24.2f%n", annualSales, person.getTotalAnnualCompensation());
相反,这将打印类似
的内容 Total Sales Amount | Total Compensation Amount
$ 6000.00 | $ 35000.00
$ 11000.00 | $ 35000.00
$ 16000.00 | $ 35000.00
$ 21000.00 | $ 35000.00
$ 26000.00 | $ 35000.00
$ 31000.00 | $ 35000.00
$ 36000.00 | $ 35000.00
$ 41000.00 | $ 35000.00
$ 46000.00 | $ 35000.00
$ 51000.00 | $ 35000.00
$ 56000.00 | $ 35000.00
现在,我不喜欢这个,我喜欢文本旁边的$
System.out.printf(" %18s | %25s%n",
NumberFormat.getCurrencyInstance().format(annualSales),
NumberFormat.getCurrencyInstance().format(person.getTotalAnnualCompensation()));
哪个打印......
Total Sales Amount | Total Compensation Amount
$6,000.00 | $35,000.00
$11,000.00 | $35,000.00
$16,000.00 | $35,000.00
$21,000.00 | $35,000.00
$26,000.00 | $35,000.00
$31,000.00 | $35,000.00
$36,000.00 | $35,000.00
$41,000.00 | $35,000.00
$46,000.00 | $35,000.00
$51,000.00 | $35,000.00
$56,000.00 | $35,000.00
现在,您可以使用NumberFormat.getInstance
来混合使用这两种方法,这样您就可以获得左边的$
和右边的数字格式,但是&# 39;由你决定...
您可能需要查看at this tutorial,其中提供了有关String
格式规范的更多详细信息