英文:
back. Any sugReturn statement printing back String and default int of 5. I only want the String to printgestions?
问题
以下是翻译好的部分:
我们正在编程课程中学习面向对象编程(OOP),这是我们的第一个作业。我遇到的问题是,它只应该返回“good”,但我却得到了“good 5”。我对所有这些仍然非常陌生,非常希望得到一些建议或修复此问题的提示。
public class Main{
public static void main(String[] args){
Dog dog = new Dog();
System.out.println(dog.checkWeight());
}
}
// 两个独立的文件 main.java 和 Dog.java
public class Dog{
String name = "unknown";
String breed = "mutt";
int weight = 5;
public int checkWeight(){
if (weight < 2){
System.out.println("under-weight");
}else if (weight > 10){
System.out.println("over-weight");
}else{
System.out.println("good");
}return weight;
}
}
英文:
We are learning oop in my programming class and this was our first assignment for it, the problem I'm having is that it is only supposed to return "good" but i'm getting back "good 5". I'm still very new with all of this and would love some suggestions or tips on how to fix this.
public class Main{
public static void main(String[] args){
Dog dog = new Dog();
System.out.println(dog.checkWeight());
}
}
//Two seperate files main.java and Dog.java
public class Dog{
String name = "unknown";
String breed = "mutt";
int weight = 5;
public int checkWeight(){
if (weight <2){
System.out.println("under-weight");
}else if (weight > 10){
System.out.println("over-weight");
}else{
System.out.println("good");
}return weight;
}
}
答案1
得分: 1
将
System.out.println(dog.checkWeight());
改为
dog.checkWeight();
目前,该方法会返回 5
并将其打印出来。
英文:
Change
System.out.println(dog.checkWeight());
to
dog.checkWeight();
Currently, the method returns 5
and you print it.
答案2
得分: 1
更改checkWeight
的定义如下:
public String checkWeight() {
String health = "";
if (weight < 2) {
health = "体重过轻";
} else if (weight > 10) {
health = "体重过重";
} else {
health = "体重正常";
}
return health;
}
英文:
Change the definition of checkWeight
as follows:
public String checkWeight() {
String health = "";
if (weight < 2) {
health = "under-weight";
} else if (weight > 10) {
health = "over-weight";
} else {
health = "good";
}
return health;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论