PrintStream类型中的printf(String,Object [])方法不适用于参数(String)

时间:2016-02-16 16:42:30

标签: java printf

package age;

import java.util.Scanner;

public class AgeTest 
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);

    Age myAge = new Age();

    System.out.printf("initial age is :%s%n%n"+ myAge.getAge());

    System.out.println("PLEASE ENTER THE AGE");
    int theAge = input.nextInt(); 
    myAge.setAge(theAge);
    System.out.println();
    // display the name stored in object myAge
    System.out.printf("age in object myAge is %n%s%n"+ myAge.getAge());

有什么问题?

1 个答案:

答案 0 :(得分:0)

我猜你的getAge()方法返回一个int。因此,尝试将myAge.getAge()解析为String,如:

String age = Integer.toString(myAge.getAge())  System.out.print("Age is: " + age );