こんにちは、これは私がプロジェクト用に作成したコードで、他のいくつかのメソッドを呼び出して、その人物に関する声明を出力します。私の問題は二重に印刷され、両方の if/else ステートメントを実行しているようです。
どうすればこれを防ぐことができますか? 必要に応じて、端末からテキストを追加できます。以下のコード。
void printPersonDescription(String name, boolean sex, int age) {
if(sex == true && yearsUntilNextMaturityLevel(age) > 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" she has " + yearsUntilNextMaturityLevel(age) + " years left until she is " +
nextMaturityLevel(age) + ".");
}
else if(sex == false && yearsUntilNextMaturityLevel(age) > 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" he has " + yearsUntilNextMaturityLevel(age) + " years left until he is " +
nextMaturityLevel(age) + ".");
}
if (sex == true && yearsUntilNextMaturityLevel(age) <= 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" she has " + yearsUntilNextMaturityLevel(age) + " year left until she is " +
nextMaturityLevel(age) + ".");
}
else {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" he has " + yearsUntilNextMaturityLevel(age) + " year left until he is " +
nextMaturityLevel(age) + ".");
}
}