我擦_你中枪了 2016-10-09 07:12 采纳率: 66.7%
浏览 1594
已采纳

Java编程问题 初学者 程序显示没有错误,但是不能运行

package bjl.petshop;

public interface Pet {

public String getName();
public String getColor();  //接口的抽象方法

}

package bjl.petshop;

public class Cat implements Pet{
private String name;
private String color;
public Cat(){

}
public Cat(String name,String color){
    this.setName(name);
    this.setColor(color);

}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getColor() {
    return color;
}
public void setColor(String color) {
    this.color = color;
}

}

package bjl.petshop;

public class PetShop {

private Pet[] pets;
private int foot;
public int len;

public PetShop(int len){
    if(len>0){
        this.pets = new Pet[len];       //数组名=new 数据类型[长度],分配内存给数组
    }
    else{
        this.pets = new Pet[1];
    }   
}

public boolean AddPet(Pet pet){
    if(this.foot < this.pets.length){
        this.pets[this.foot] = pet;
        this.foot++;
        return true;
    }
    else{
        return false;
    }
}

public boolean SearchPet(String keyWord){
    boolean count = false;
    for(int i=0;i<=this.pets.length;i++){


            if(this.pets[i].getName().equals(keyWord)){
                count = true;
            }
            else{
                count = false;
            }
        }
    return count;

}

}

package bjl.petshop;

public class Test {

public static void main(String args[]){
    PetShop ps = new PetShop(6);

    ps.AddPet(new Cat("小白","白色"));
    ps.AddPet(new Cat("小黑","黑色"));
    ps.AddPet(new Cat("小花","花色"));
    ps.AddPet(new Cat("小红","红色"));
    ps.AddPet(new Cat("小黄","黄色"));

    System.out.println(ps.SearchPet("小白"));


}

}

  • 写回答

2条回答 默认 最新

  • WxjMLove 2016-10-09 07:54
    关注

    public boolean SearchPet(String keyWord){
    boolean count = false;
    for(int i=0;i<=this.pets.length;i++){

            if(this.pets[i].getName().equals(keyWord)){
                count = true;
            }
            else{
                count = false;
            }
        }
    return count;
    

    }
    你这里逻辑有问题吧

    public boolean SearchPet(String keyWord){
    boolean count = false;
    for(int i=0;i<=this.pets.length;i++){

            if(this.pets[i].getName().equals(keyWord)){
                count = true;
                                break;
            }
            else{
                count = false;
            }
        }
    return count;
    

    }你跳出下试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型