Java 返回任何sugreurn语句打印回字符串,默认int为5。我只想把字符串打印出来?
我们在编程课上学习oop,这是我们的第一个作业,我遇到的问题是,它应该只返回“好”,但我返回“好5”。我对这一切还很陌生,希望能得到一些关于如何解决这一问题的建议或提示Java 返回任何sugreurn语句打印回字符串,默认int为5。我只想把字符串打印出来?,java,oop,return,Java,Oop,Return,我们在编程课上学习oop,这是我们的第一个作业,我遇到的问题是,它应该只返回“好”,但我返回“好5”。我对这一切还很陌生,希望能得到一些关于如何解决这一问题的建议或提示 public class Main{ public static void main(String[] args){ Dog dog = new Dog(); System.out.println(dog.checkWeight()); } } //两个独立的文件main.java和Dog.java public
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;
}
}
公共级狗{
String name=“未知”;
String-breed=“mutt”;
整数权重=5;
公共整数校验权重(){
如果(重量10){
系统输出打印项次(“超重”);
}否则{
系统输出打印号(“良好”);
}返回重量;
}
}
更改
System.out.println(dog.checkWeight());
到
当前,该方法返回
5
,您可以打印它。更改检查权重的定义,如下所示:
public String checkWeight() {
String health = "";
if (weight < 2) {
health = "under-weight";
} else if (weight > 10) {
health = "over-weight";
} else {
health = "good";
}
return health;
}
公共字符串校验权重(){
字符串health=“”;
如果(重量<2){
health=“体重不足”;
}否则,如果(重量>10){
health=“超重”;
}否则{
health=“good”;
}
恢复健康;
}
我的教授需要一个System.out.println。。。这一切都是在repl.it上完成的
public String checkWeight() {
String health = "";
if (weight < 2) {
health = "under-weight";
} else if (weight > 10) {
health = "over-weight";
} else {
health = "good";
}
return health;
}