gpt4 book ai didi

java - 如何在条件下设置/调用对象和方法

转载 作者:行者123 更新时间:2023-12-02 11:08:02 24 4
gpt4 key购买 nike

如何在条件中设置对象和方法?据我了解,如果动物超过 50 公斤,则重量过重。但是,如果动物饥饿需要爱并且感到无聊返回方法feelingNegative()呢?我不知道如何设置它。但动物 sleep 后就会感到饥饿。一个想法是:

Animal {
if (hangry == false && needLove == false && boring == false) {
return feelingNegative();
}
}

还是不知道如何设置。

public class Animal {

private boolean needLove;
private boolean hangry;
private boolean boring;
private int kg;

public boolean sleep() {
return hangry = true;
}

public boolean watchTv() {
return needLove = true;
}

public void feelingPositive() {
System.out.println("I feel good");
}

public void feelingNeutral() {
System.out.println("Someting is missing...");
}

public void feelingNegative() {
System.out.println("I need love, food and fun!");
}

public void weight(int kg) {
if(50 < kg) {
System.out.println("You ate way too much");
}else {
System.out.println("You need to eat more");
}
}
}

最佳答案

您调用的方法不会返回任何内容(它们是void)。只需删除返回即可。并使用 boolean 否定 (!) 而不是 == false。就像,

if (!hangry && !needLove && !boring) {
feelingNegative();
}

关于java - 如何在条件下设置/调用对象和方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50787487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com