简体   繁体   English

我不知道怎么了

[英]I don't know what's wrong

if(handler.obj.isEmpty())
    handler.addObject(new Box(x, y, ID.Box));
else{
    for(int i = 0; i < handler.obj.size(); i++){
        Object tempObj = handler.obj.get(i);
        if (tempObj.getX() == x && tempObj.getY() == y && tempObj.getId() == ID.Box)
            handler.removeObect(tempObj);
        else
            handler.addObject(new Box(x, y, ID.Box));
    }
}

the handler.addObject() in the else statement seems unreachable or doesn't work else语句中的handler.addObject()似乎无法访问或不起作用

From comment:来自评论:

public class Handler {
    LinkedList<Object> obj = new LinkedList<Object>();
    public void tick(){
        for (int i = 0; i < obj.size();i++){
            Object tempObj = obj.get(i);
            tempObj.tick();
        }
    }
    public void render(Graphics g){
        for (int i = 0; i < obj.size(); i++){
            Object tempObj = obj.get(i);
            tempObj.render(g);
        }
    }
    public void addObject(Object obj){
        this.obj.add(obj);
    }
    public void removeObect(Object obj){
        this.obj.remove(obj);
    }
}

Let's see.让我们来看看。 Assume first time it runs, x,y is 1,1 .假设它第一次运行时, x,y1,1 So you add Box(1,1) .所以你添加Box(1,1)

Next time is run, let's assume x,y is 2,3 .下次运行时,假设x,y2,3 So, enter for loop:因此,进入for循环:

  • i = 0 : Not same x,y , so enter else , and add Box(2,3) . i = 0 :不相同的x,y ,所以输入else ,并添加Box(2,3)
  • i = 1 : Same as x,y (yeah, we just added it), so enter if and remove Box(2,3) . i = 1 :与x,y相同(是的,我们刚刚添加了它),因此输入if并删除Box(2,3)

Result: Box added and removed again.结果:再次添加和删除框。

Oh yeah, a debugger would have told you the same thing.哦,是的,调试器会告诉你同样的事情。

Your problem is that your for loop ends in the wrong place.你的问题是你的for循环在错误的地方结束。 This causes two undesirable symptoms.这会导致两种不良症状。

  • Firstly, you're adding your new Box once for every element in your list that doesn't match the box, instead of adding it just once.首先,您要添加新的Box一次列表中的所有元素匹配的盒子,而不是将它添加一次。
  • Secondly, you're removing some of those new Box objects that you just added, once you get to the end of the loop.其次,一旦到达循环末尾,您将删除一些刚刚添加的新Box对象。

I would recommend rewriting the method like this.我建议像这样重写方法。 Notice how the for loop ends before the new object is added - this makes sure that the object is added at most once.请注意for循环如何在添加新对象之前结束 - 这确保最多添加一次对象。 Also notice the return statement after an object is removed - this can be done because once the object is removed, the method has no more work to do.还要注意删除对象后的return语句 - 可以这样做,因为一旦删除了对象,该方法就没有更多工作要做。

public void addOrRemoveBox(int x, int y) {
    for(int i = 0; i < handler.obj.size(); i++){
        Object tempObj = handler.obj.get(i);
        if (tempObj.getX() == x && tempObj.getY() == y && tempObj.getId() == ID.Box) {
            handler.removeObect(tempObj);
            return;
         }
    }
    handler.addObject(new Box(x, y, ID.Box));
}

Lastly, you appear to have written your own class called Object (otherwise this code wouldn't compile).最后,您似乎编写了自己的名为Object的类(否则此代码将无法编译)。 This is probably a bad idea, because it will cause you to get confused between your Object class, and the Object class that's built into Java.这可能是一个坏主意,因为它会导致你得到你的困惑与Object类和Object是内置到Java类。 I suggest you rename that class.我建议你重命名那个类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:遇到 ConcurrentModificationException。 我的迭代器和/或 HashMap 有问题,我不知道它是什么 - Java: Encountering ConcurrentModificationException. There's something wrong with my iterators and/or HashMaps, and I don't know what it is java.lang.NullPointerException,我不知道怎么了 - A java.lang.NullPointerException, and I don't know what's wrong 我的 JDBC 有问题,但我不知道为什么 - There's something wrong with my JDBC, but I don't know why 我不知道我的一个变量有什么问题。[线程“main”中的异常java.util.UnknownFormatConversionException:Conversion =&#39;m&#39;] - I don't know what's wrong with one of my variable.[Exception in thread “main” java.util.UnknownFormatConversionException: Conversion = 'm'] 不知道我的代码有什么问题。 Java GUI - Don't know what's wrong with my code. Java GUI SQLite 异常:“)”附近:语法错误(代码 1 SQLITE_ERROR),不知道出了什么问题 - SQLite Exception: near ")": syntax error (code 1 SQLITE_ERROR), don't know what's wrong 有时我不知道某些方法属于哪些类。 我究竟做错了什么? - Sometimes I don't know which classes certain methods belong to. What am I doing wrong? 我的手电筒应用程序运行缓慢并且崩溃,我不知道自己在做什么错 - My flashlight app is slow and crashes allot i don't know what I'm doing wrong Java的String .replaceFirst无法正常工作,我不知道它是否是错误或我做错了 - Java's String .replaceFirst is not working and I don't know if its a bug or I did it wrong 我试图制作一个程序,但控制台仍显示“线程“ main”中的异常”“ java.lang.NullPointerException”,我不知道这是怎么回事 - I've tried to make a program, but console still shows “Exception in thread ”main“ java.lang.NullPointerException” and I don't know what's wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM