java - 循环代码导致堆栈溢出

标签 java loops while-loop stack-overflow

我有这个代码:

 public static void Detect (String[] args) throws Exception {

        PointerInfo pointer; /* needed for getting cursor location */
        pointer = MouseInfo.getPointerInfo();
        Point coord = pointer.getLocation();

        Robot cursor = new Robot(); /*Creates a new robot */
        cursor.delay(500); /* robot delay */

        /**
         * detection method
         * Works by looking at pixel color underneath mouse.
         * If RED is over > a value and GREEN is under < a value then loop
         * If criteria is not matched go to Something
         */
        while(true) {
            coord = MouseInfo.getPointerInfo().getLocation();       
            Color color = cursor.getPixelColor((int)coord.getX(), (int)coord.getY());
            if(color.getRed() >= 75 && color.getGreen() < 100 ){
                Detect(args);

            }
            else{        
                System.out.println(color);
                Something(args);


            }
            cursor.delay(1000);

        }
   }

我知道这可能是最糟糕的实现。调用 void 来创建循环会导致堆栈溢出。有人可以解释一下我如何使整个代码段做同样的事情但使用“while”循环吗?

这是堆栈跟踪顺便说一句:

Exception in thread "main" java.lang.StackOverflowError
at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source)
at java.awt.MouseInfo.areScreenDevicesIndependent(Unknown Source)
at java.awt.MouseInfo.getPointerInfo(Unknown Source)
at com.meganukebmp.Main.Detect(Main.java:29)
at com.meganukebmp.Main.Detect(Main.java:45)
at com.meganukebmp.Main.Detect(Main.java:45)
at com.meganukebmp.Main.Detect(Main.java:45)
at com.meganukebmp.Main.Detect(Main.java:45)
at com.meganukebmp.Main.Detect(Main.java:45)
at com.meganukebmp.Main.Detect(Main.java:45)
at com.meganukebmp.Main.Detect(Main.java:45)
at com.meganukebmp.Main.Detect(Main.java:45)
at com.meganukebmp.Main.Detect(Main.java:45)
at com.meganukebmp.Main.Detect(Main.java:45)

最佳答案

如果

if(color.getRed() >= 75 && color.getGreen() < 100 )

已满,您将无限调用 Detect 方法。

我建议您检查您的 RGB 值,如果它始终返回 true,请更正测试。

来源:Recursion

The job of the recursive cases can be seen as breaking down complex inputs into simpler ones. In a properly designed recursive function, with each recursive call, the input problem must be simplified in such a way that eventually the base case must be reached. (Functions that are not intended to terminate under normal circumstances—for example, some system and server processes—are an exception to this.) Neglecting to write a base case, or testing for it incorrectly, can cause an infinite loop.

关于java - 循环代码导致堆栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33209610/

相关文章:

java - 使用 Java/EJB 2.x 在 Oracle 上按日期(1916 年 5 月 1 日)查询记录

c - 如何根据用户的输入/选择打印和编辑数组列表中的特定元素?

c++ - 为什么无论我输入什么,我的 "while"循环都会执行?

node.js - 使用 Node 和 Mongoose 从数组中生成新的 mongoDB 文档的最佳方法是什么?

c - 我怎样才能只计算列表中的某些项目?

php - 如何在循环中创建换行符

C编程-关于while循环再次提示用户问题

java - 从 C 调用 Java

java - 卡格尔算法

java - Picocli:为 Map 类型的选项指定默认值