我的第一堂课......
import java.util.Scanner;
class MethodsANDInstances16A {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MethodsANDInstances16B methObject = new MethodsANDInstances16B();
System.out.println("Enter name of first girlfriend or good friend here: ");
String temp = input.nextLine();
methObject.setName(temp);
methObject.saying();
}
}
我的第二堂课......
public class MethodsANDInstances16B {
private String girlName;
public void setName(String name){
girlName = name;
}
public String getName(){
return girlName;
}
public void saying(){
System.out.printf("Your first girlfriend or good friend was %s", getName());
}
}
我刚刚在YouTube上观看了基本完全相同代码的教程......但是printf语句对我不起作用......但它在我观看的视频中有效。我听说我需要更改Eclipse中的设置(我正在使用的IDE),但我不确定?
这是我运行时遇到的错误......
"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String)
at MethodsANDInstances16B.saying(MethodsANDInstances16B.java:11)
at MethodsANDInstances16A.main(MethodsANDInstances16A.java:10)"
谢谢。
答案 0 :(得分:-1)
您需要提供区域设置。
public PrintStream printf(Locale, String, Object... args)
所以请使用它。
System.out.printf(Locale.ENGLISH, "Girlfriend or what not was %s", getName())