陷入 while 循环

发布于 2024-11-26 07:45:21 字数 2441 浏览 1 评论 0原文

我有一个像这样的简单代码:

try {
    int i = 0;
    while (i < size) {
        System.out.println("i is" + i);
        if (someCondition) {
            System.out.println("do sth");
            someCondition = true;
            i++;
            System.out.println("i is" + i);
        } else {
            System.out.println("doAnotherThing");
            someCondition = true;
            i++;
            System.out.println("i is" + i);
        }
    }
} catch(Exception){

}

该代码片段的输出是:

i is 0
do sth
i is 1
i is 0
doAnotherThing
i is 1

它应该增加 I,而不是中断 while 循环,但事实并非如此。对于这个问题你有什么看法吗?我已经研究这个问题 5 个小时了,也许我遗漏了一些东西。如果你能帮助我,我会很高兴。
提前致谢。

编辑:我想简化事情,但看起来它不起作用:)好的这是真正的代码:

public void (Analyzer analyzer){
    try {    
        int i=0;
        while (i < analyzer.size()) {
            System.out.println("i is" + i);
            Object anInstance = analyzer.getObject().get(i);
            if (anInstance.getDatabaseCreated()) { //this comes from another class and is //false in the first place                         
                dropObject(analyzer.getId(),i); // removes object
                createAnInstance(analyzer.getId(), i, anInstance.getTypes()); //creates another instance                         
                anInstance.markCreated();

                Query query = createInsertQuery(analyzer.getId(), i, anInstance.getTypes());                          

                for (int j = 0; j < anInstance.rowCount(); j++) {
                    insertRow(query, anInstance.getTypes(), anInstance.getRow(j));
                }
                i++;
                System.out.println("i is" + i);
            } else {                            
                createAnInstance(analyzer.getId(), i, anInstance.getTypes());
                anInstance.markCreated();
                Query query = createInsertQuery(analyzer.getId(), i, anInstance.getTypes());
                for (int j = 0; j < anInstance.rowCount(); j++) {
                    insertRow(query, anInstance.getTypes(), anInstance.getRow(j));                                   
                }
                i++;
                System.out.println("i is" + i);
            }
        }
    } catch (Exception ex) {
        Logger.getLogger(AnalyzerService.class.getName()).log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
}

I have a simple code like this:

try {
    int i = 0;
    while (i < size) {
        System.out.println("i is" + i);
        if (someCondition) {
            System.out.println("do sth");
            someCondition = true;
            i++;
            System.out.println("i is" + i);
        } else {
            System.out.println("doAnotherThing");
            someCondition = true;
            i++;
            System.out.println("i is" + i);
        }
    }
} catch(Exception){

}

This code snippet's output is:

i is 0
do sth
i is 1
i is 0
doAnotherThing
i is 1

It should have increase I, than break while loop but it doesn't. Do you have any opinion about this issue? I am working on this issue since 5 hours, maybe I am missing something. I will be glad if you can help me.
Thanks in advance.

EDIT: I wanted to simplify things, but appearantly it didn't work :) OK Here is the real code:

public void (Analyzer analyzer){
    try {    
        int i=0;
        while (i < analyzer.size()) {
            System.out.println("i is" + i);
            Object anInstance = analyzer.getObject().get(i);
            if (anInstance.getDatabaseCreated()) { //this comes from another class and is //false in the first place                         
                dropObject(analyzer.getId(),i); // removes object
                createAnInstance(analyzer.getId(), i, anInstance.getTypes()); //creates another instance                         
                anInstance.markCreated();

                Query query = createInsertQuery(analyzer.getId(), i, anInstance.getTypes());                          

                for (int j = 0; j < anInstance.rowCount(); j++) {
                    insertRow(query, anInstance.getTypes(), anInstance.getRow(j));
                }
                i++;
                System.out.println("i is" + i);
            } else {                            
                createAnInstance(analyzer.getId(), i, anInstance.getTypes());
                anInstance.markCreated();
                Query query = createInsertQuery(analyzer.getId(), i, anInstance.getTypes());
                for (int j = 0; j < anInstance.rowCount(); j++) {
                    insertRow(query, anInstance.getTypes(), anInstance.getRow(j));                                   
                }
                i++;
                System.out.println("i is" + i);
            }
        }
    } catch (Exception ex) {
        Logger.getLogger(AnalyzerService.class.getName()).log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

扶醉桌前 2024-12-03 07:45:21

我认为为了获得您给出的输出,必须对该方法进行两次单独的调用(在上面的代码片段中没有名称)。看起来该方法被调用,i被设置为0,循环开始并且第一个条件为真。然后循环结束(因为analyzer.size()是1,或者可能是一个异常???)然后在代码的其他地方再次调用这个方法。 i 再次设置为 0,但这次第一个条件为 false(第一次调用该方法的结果???),因此您改为执行 else。同样,循环在 1 次迭代后结束,要么因为analyzer.size() 为 1,要么抛出异常)。

I think that in order to get the output you have given there must be 2 separate calls being made to the method (which in your code snippet above has no name). It seems the method is called, i is set to 0, the loop starts and the first condition is true. The loop then ends (because analyzer.size() is 1, or maybe an exception???) then somewhere else in your code you are calling this method again. Again i is set to 0 but this time the first condition is false (a result of the first call to the method???) so you go through the else instead. Again the loop ends after 1 iteration, either because analyzer.size() is 1 or an exception is thrown).

在梵高的星空下 2024-12-03 07:45:21

为什么在 ifelse 中将 someCondition 设置为 true ?另外,检查 someCondition 是否将 i 的值重置为 0 或其他值。如果不知道 someCondition 是什么,就很难猜测。但如果它是一个将 i 的值作为参数的函数,我会检查它。

Why are you setting the someCondition to true in both if and else ? Also, check if the someCondition is resetting the value of i to 0 or something. Without knowing what the someCondition is, it is difficult to guess. But if it is a function taking the value of i as a a parameter, I will check that.

葵雨 2024-12-03 07:45:21

这段代码是假的——括号甚至没有对齐。正如所写,假设您初始化了 size 变量,这将起作用,否则 size 可能很大(随机整数,如 384923492348),并且会进行大量迭代。如果您想要更好的答案,您可以修复代码吗(只需复制粘贴?:))

This code's bogus - the bracketing doesn't line up even. As written this would work assuming you initialized the size variable, otherwise size might be huge (random integer number like 384923492348) and it would do alot of iterations. Can you fix the code up if you want a better answer (just copy paste? :) )

脸赞 2024-12-03 07:45:21

在我看来,dropObject正在改变analyzer.size(),而后者的大小首先是1

It seems to me that dropObject is altering analyzer.size() and that the size of the latter was 1 in the first place.

任性一次 2024-12-03 07:45:21

很明显,analyzer.size()正在发生变化。由于我不知道其余的来源,因此我无法确定是什么原因造成的。 的列表:

  • analyzer.sizeanalyzer.getObject
  • ().getanInstance.getDatabaseCreateddropObjectmarkCreatedanalyzer.getIdanalyzer.getTypesanInstance.getRowanInstance.getTypesinsertRow
  • 如果
  • 什么
  • 以下是
  • 可能
  • 罪魁祸首
  • 不需要

担心当您向查询添加行,您可以简单地提前缓存分析器大小,但您可能会遇到麻烦,因为看起来您正在删除一些值,同时添加其他值。不过,如果这并不重要,那就很简单:

int i=0;
int sz = analyzer.size()
while (i < sz) {

另外,我注意到有很多复制和粘贴的代码。糟糕的交易。你的整个 while 循环可能是:

// you left out the function name!
public void theNamelessOne(Analyzer analyzer){
    try {    
        int i=0;
        while (i < analyzer.size()) {
            System.out.println("i is" + i);
            // the only time you should really use an article ("a", "an", "the" 
            // or equivalent) in a method or vaeriable name is when referencing 
            // "the doctor" or "the wabbit". since there is only one "the doctor"
            // and this is not an Elmer Fudd related instance, you may want to 
            // remove it.
            Object anInstance = analyzer.getObject().get(i);
            if (anInstance.getDatabaseCreated()) { 
                dropObject(analyzer.getId(),i);
            } 
            createAnInstance(analyzer.getId(), i, anInstance.getTypes());
            anInstance.markCreated();
            Query query = createInsertQuery(  analyzer.getId(), 
                                              i, 
                                              anInstance.getTypes());
            for (int j = 0; j < anInstance.rowCount(); j++) {
                insertRow(query, anInstance.getTypes(), anInstance.getRow(j));
            }
            i++;
            System.out.println("i is" + i);
        }
    } catch (Exception ex) {
        Logger.getLogger(
            AnalyzerService.class.getName()
        ).log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
}

It's apparent that analyzer.size() is mutating. Since I don't know the rest of the source, I can't know for sure what's causing it. Here's a list of possible culprits:

  • analyzer.size
  • analyzer.getObject().get
  • anInstance.getDatabaseCreated
  • dropObject
  • markCreated
  • analyzer.getId
  • analyzer.getTypes
  • anInstance.getRow
  • anInstance.getTypes
  • insertRow

If you don't need to worry about what happens when you add lines to the query, you could simply cache analyzer size ahead of time, but you might run into trouble as it looks like you are removing some values while simultaneously adding others. If this doesn't matter, though, it is as simple as:

int i=0;
int sz = analyzer.size()
while (i < sz) {

Also, I noticed that there was a lot of copied and pasted code. Bad deal. Your entire while loop could be:

// you left out the function name!
public void theNamelessOne(Analyzer analyzer){
    try {    
        int i=0;
        while (i < analyzer.size()) {
            System.out.println("i is" + i);
            // the only time you should really use an article ("a", "an", "the" 
            // or equivalent) in a method or vaeriable name is when referencing 
            // "the doctor" or "the wabbit". since there is only one "the doctor"
            // and this is not an Elmer Fudd related instance, you may want to 
            // remove it.
            Object anInstance = analyzer.getObject().get(i);
            if (anInstance.getDatabaseCreated()) { 
                dropObject(analyzer.getId(),i);
            } 
            createAnInstance(analyzer.getId(), i, anInstance.getTypes());
            anInstance.markCreated();
            Query query = createInsertQuery(  analyzer.getId(), 
                                              i, 
                                              anInstance.getTypes());
            for (int j = 0; j < anInstance.rowCount(); j++) {
                insertRow(query, anInstance.getTypes(), anInstance.getRow(j));
            }
            i++;
            System.out.println("i is" + i);
        }
    } catch (Exception ex) {
        Logger.getLogger(
            AnalyzerService.class.getName()
        ).log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文