Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回任何sugreurn语句打印回字符串,默认int为5。我只想把字符串打印出来?_Java_Oop_Return - Fatal编程技术网

Java 返回任何sugreurn语句打印回字符串,默认int为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

我们在编程课上学习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 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;
}