日期:2014-05-20  浏览次数:20738 次

面对对象简单题目,大家帮我看看
一共两个类

class Employee{
   /* Declare three instance variables here. */
private String firstName;
private String lastName;
private double salary;
   /* Add a constructor that declares a parameter for each instance variable. Assign
      each parameter value to the appropriate instance variable. Write code that
      validates the value of salary to ensure that it is not negative. */
public Employee(String nameOfFirst, String nameOfLast, double monthlySalary){
firstName = nameOfFirst;
lastName = nameOfLast;
salary = monthlySalary;
}
   /* Declare set and get methods for the first name instance variable. */
public void setFirstName(String nameOfFirst){
firstName = nameOfFirst;
}
public String getFirstName(){
return firstName;
}
   /* Declare set and get methods for the last name instance variable. */
public void setLastName(String nameOfLast){
lastName = nameOfLast;
}
public String getLastName(){
return lastName;
}
   /* Declare set and get methods for the monthly salary instance variable. Write code
      that validates the salary to ensure that it is not negative. */
public void setSalary(double monthlySalary){
if(monthlySalary > 0.00)
salary = monthlySalary;
}
public double getSalary(){
return salary;
}
}/* End class declaration of Employee class. */



class EmployeeTest{

   /* Begin main method declaration. */
public static void main(String[] args){
   /* Create two Employee objects and assign them to Employee variables. */
Employee employee1 = new Employee("Bob","Jones",2875.00);
Employee employee2 = new Employee("Susan","Baker",3150.75);
   /* Output the first name, last name and salary for each Employee. */
System.out.printf("Employee 1: %s %s; Yearly Salary: %.2f\n",
employee1.getFirstName(),employee1.getLastName(),employee1.getSalary()*12);
System.out.printf("Employee 2: %s %s; Yearly Salary: %.2f\n\n",
employee2.getFirstName(),employee2.getLastName(),employee2.getSalary()*12);
   /* Give each Employee a 10% raise. */

System.out.printf("Increasing employee salaries by 10%\n");
   /* Output the first name, last name and salary of each Employee again. */
System.out.printf("Employee 1: %s %s; Yearly Salary: %.2f\n",
employee1.getFirstName(),employee1.getLastName(),employee1.getSalary()*13.2);
System.out.printf("Employee 2: %s %s; Yearly Salary: %.2f\n\n",
employee2.getFirstName(),employee2.getLastName(),employee2.getSalary()*13.2);
}/* End main method declaration */

}/* End class declaration of EmployeeTest class. */


我感觉是没有错误的 但是在eclipse下运行的结果是


Employee 1: Bob Jones; Yearly Salary: 34500.00
Employee 2: Susan Baker; Yearly Salary: 37809.00

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '
'
at java.util.Formatter.checkText(Formatter.java:2547)
at java.util.Formatter.parse(Formatter.java:2533)
at java.util.Formatter.format(Formatter.java:2469)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at EmployeeTest.main(EmployeeTest.java:19)


不知道是什么原因 大家帮忙看看
java

------解决方案--------------------
你换成 System.out.println试试,看样子是在自己变format的时候出了点问题
------解决方案--------------------
 System.out.printf("Increasing employee salaries by 10%\n");
因为那个百分号,你应该使用成\u0025

具体原因请查询jdk文档的:
java.lang.Object
  java.util.Formatter