【问题标题】:The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String) [duplicate]PrintStream 类型中的方法 printf(String, Object[]) 不适用于参数 (String) [重复]
【发布时间】:2016-05-28 01:29:53
【问题描述】:
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());

怎么了?

【问题讨论】:

  • 错误信息已经够清楚了。你有什么不明白的?
  • 这是我第一次使用 java :)

标签: java printf


【解决方案1】:

我猜你的 getAge() 方法返回一个 int。所以尝试将 myAge.getAge() 解析为字符串,例如:

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

【讨论】:

  • 为此更改您的 System.out.printf("initial age is :%s%n%n"+ myAge.getAge()); 行 ----> System.out.printf("initial age is :%s%n%n"+ Integer.toString(myAge.getAge())); 并在最后一个 printf 中执行相同操作。
猜你喜欢
  • 2017-08-15
  • 2012-06-24
  • 2011-12-08
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 2013-12-19
  • 2018-01-09
相关资源
最近更新 更多