英文:
Setting a boolean value in Constructor, changing it in a method, return it in another
问题
public class Doggo {
private String name;
private boolean goodBoi;
public Doggo(String name){
this.name = name;
}
public String getName() {
return name;
}
public void makeBark() {
System.out.println(name + " said: Woof woof");
}
// Constructor to set the 'goodBoi' value to false
public Doggo() {
this.goodBoi = false;
}
// Method to set the 'goodBoi' value to true
public void makeGoodBoi() {
this.goodBoi = true;
}
// Method to check and return the value of 'goodBoi'
public boolean isGoodBoi() {
return goodBoi;
}
public void whosAGoodBoi() {
if (isGoodBoi()) {
System.out.println(name + " is such a good boii");
} else {
System.out.println(name + " is not a good boi :(");
}
}
public static void main(String[] args) {
Doggo dog = new Doggo("Casper");
System.out.println(dog.getName());
dog.makeBark();
// Set the dog as a good boi and check its status
dog.makeGoodBoi();
dog.whosAGoodBoi();
}
}
请注意,我已经根据你的要求,只返回翻译后的代码部分,没有其他内容。如果你有任何关于代码的疑问,请随时提问。
英文:
I'm a newbie yet, so please feel free to accuse me of asking silly things xD. I just started coding. So I want to specify my question to make it clear for you. I'm stuck regarding this: We need a constructor (public DoggoII) which sets our value to false. Then we need a method (makeGoodBoi()) to set the value to true and then I need another method (isGoodBoi()) to return the value of the private field goodBoi and System.out.print some stuff later. Consider the rest of the code as done. Can someone give me a hint or something on how to do that? Because I'm kinda lost.
The question is if I have a fault that I can't find and how to return a boolean value in another method in general. Thanks!
public class Doggo {
private String name;
private boolean goodBoi;
public Doggo(String name){
this.name = name;
}
public String getName() {
return name;
}
public void makeBark() {
System.out.println(name + " said: Woof woof");
}
public Doggo (boolean goodBoi){
this.goodBoi= false;
}
public void makeGoodBoi(){
this.goodBoi = !this.goodBoi;
}
public void isGoodBoi(){
if (makeGoodBoi()){
return;
}
}
public void whosAGoodBoi() {
if (isGoodBoi()) {
System.out.println(name + " is such a good boii");
} else {
System.out.println(name + " is not a good boi :(");
}
}
public static void main(String[] args) {
Doggo dog = new Doggo("Casper");
System.out.println(dog.getName());
dog.makeBark();
}
}
答案1
得分: 1
只是一个基本的获取器,将返回类型更改为 boolean
,而不是 void
。
public boolean isGoodBoi() {
return goodBoi;
}
英文:
Just a basic getter, use boolean
as return type instead of void
.
public boolean isGoodBoi() {
return goodBoi;
}
答案2
得分: 1
由于goodBoi
是一个类成员,并且默认情况下布尔类成员为false,所以您除了添加一个getter之外,无需做任何事情
public boolean isGoodBoi() {
return goodBoi;
}
这将返回类成员的当前值。
因此,获取这个值就像这样简单:
DOGGO_OBJECT.isGoodBoi();
> 接下来我们需要一个方法(makeGoodBoi()
)将值设置为true,然后我需要另一个方法(isGoodBoi()
)来返回私有字段goodBoi
的值,并且稍后使用System.out.print
打印一些内容。
public void makeGoodBoi() {
this.goodBoi = true;
}
英文:
Since goodBoi
is a class member and by default boolean class members are false, so you don’t have to do anything except add a getter
public boolean isGoodBoi() {
return goodBoi;
}
This will send whatever current value of class member is.
So getting this would be as simple as;
DOGGO_OBJECT.isGoodBoi();
> Then we need a method (makeGoodBoi()) to set the value to true and then I need another method (isGoodBoi()) to return the value of the private field goodBoi and System.out.print some stuff later.
public void makeGoodBoi() {
this.goodBoi = true;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论