我在java中使用For循环做错了什么?

时间:2017-12-16 12:04:17

标签: java loops object for-loop return

我一直在四处寻找这个问题的答案,我似乎无法弄明白。我认为问题似乎与我的代码的“范围”有关,但我不知道我做错了什么,我真的可以使用一些帮助。我是Java的新手。

我的目标

  1. 在添加对象之前测试一个对象是否已经存在。

  2. 如果对象已存在于数组中,则返回null。

  3. 如果数组中不存在,请创建它然后将其返回。

  4. 守则

        public Business addBusiness(String person, String business, String location) {
        int id = 0;
    
        for (Business business : businesses ) {
            if (business.getPerson().equals(person)) {
                if (business.getBusiness().equals(business)) {
                    if (business.getLocation().equals(location)) {
                        return null;
                    }
                }
            }
        }
        Business newBusiness = new Business(person, business, location, id);
        return newBusiness;
    }
    

    运行时会发生什么

    当我运行代码时,它将很乐意在具有相同细节的对象之后创建对象。这让我相信我的逻辑存在问题。我期待当它返回null时,那将是它的结束。

    我很感激你能解决这个问题的任何提示。

    编辑:“商务”课程

    public class Business {     
    // Attributes //
    private String person, business, location;
    private int id;
    
    // Constructor //
    
    public Business(String person, String business, String location, int id) {
        this.person = person;
        this.business = business;
        this.location = location;
        this.id = id;
    }
    
    // Getters //
    
    public String getPerson() {
        return person;
    }
    
    public String getBusiness() {
        return business;
    }
    
    public String getLocation() {
        return location;
    }
    
    public int getId() {
        return id;
    }
    }
    

2 个答案:

答案 0 :(得分:1)

尝试

public Business addBusiness(String person, String business, String location) 
{
    int id = 0;

    for (Business b : businesses ) {
        if (b.getPerson().equals(person)) {
            if (b.getBusiness().equals(business)) {
                if (b.getLocation().equals(location)) {
                    return null;
                }
            }
        }
    }
    Business newBusiness = new Business(person, business, location, id);
    return newBusiness;
}

您正在使用您在循环中迭代的对象从函数的签名中屏蔽参数business。将其重命名为b可以避免此问题。

答案 1 :(得分:1)

好的 - 所以我认为我明白了。非常感谢大家对此事的帮助。

public Business addBusiness(String person, String business, String location) 
{
int id = 0;

for (Business business : businesses ) {
    if (business.getPerson().equals(person)) {
        if (business.getBusiness().equals(business)) {
            if (business.getLocation().equals(location)) {
                return null;
            }
        }
    }
}
Business newBusiness = new Business(person, business, location, id);
return newBusiness;
}

问题的原因是因为我一直没有将newBusiness对象添加到数组中。所以每次运行它都不会匹配现有的条目,因为数组中没有条目。

正确的代码应该是:

public Business addBusiness(String person, String business, String location) 
{
int id = 0;

for (Business b : businesses ) {
    if (b.getPerson().equals(person)) {
        if (b.getBusiness().equals(business)) {
            if (b.getLocation().equals(location)) {
                return null;
            }
        }
    }
}
Business newBusiness = new Business(person, business, location, id);
b.add(newBusiness); // This was the missing line, now it seems to work fine.
return newBusiness;
}

显然,我还有很多需要学习的东西。