我有點困惑。以前我發佈了一個問題:Conventional Programming: How do you return two Types of primitive ('int and 'string') from "get" method?如何使用String.format打印出兩個對象()
它得到的答案是使用String.format() with format specifiers (%s, %d, %f...)
。
雖然答案是有點混亂,我明白,%F意味着轉換(?)
但你如何打印出age
和name
使用這種方法。我的嘗試:從上述方法被執行
// DISPLAY Person name and age
public String displayProfile() {
return String.format(%fgetAge(), %fgetName())
}
主要方法:
if (optionSelected == 2) {
System.out.println(zac.displayProfile());
}
我遍這個地方,任何幫助,將不勝感激。
更新: 根據要求,我發佈了使用String.format()方法的get和set方法。因爲雖然在糾正String.format方法後我沒有收到任何錯誤,但是在從String.format()返回getAge()和GetName()後,它不會打印出'name'和'age'。我在做什麼錯誤是什麼錯誤?
// SET NAME
public void setName(String name) {
this.name = name;
}
// GET NAME
public String getName() {
return this.name;
}
// SET AGE
public void setAge(int age) {
this.age = age;
}
// GET AGE
public int getAge() {
return this.age;
}
嘿,夥計們!所以我做了改動,沒有發現任何錯誤。但是,執行main方法時,我不打印出「名稱」或甚至「年齡」:if(optionSelected == 2){System.out.println(zac。displayProfile()); } – user3289740