有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java程序在试图分析字符串时冻结

我正在写一个方法,它应该分析用户给出的多项式(作为字符串),并在将来用它做不同的事情。当时,我正试图测试我目前掌握的代码,但每当我执行程序时,它就会冻结,在电脑前坐了几个小时后,我仍然找不到其中的罪犯

我在测试是否可以分析一个变量的多项式,然后重新打印,但它不起作用

我希望有人能帮我解决这个问题

这是执行该方法的主代码块,字符串userInput是一个多项式(例如4x-6x^2):

String userInput = inputArea.getText().trim();
Monomials monomials = new Monomials();
monomials.analyse(userInput);

下面是monomials类及其方法analysis():

//Class Monomial
class Monomials
{           
    private int coeff = 0;
    private char var;
    private int addpow = 1;
    private int pow;
    private char powsign = '^';
    private char minus = '-';
    private boolean isnegative = false;
    private String mono;

    StringBuilder stringBuilder = new StringBuilder();

    public int getCoeff(int coeff)
    {
        return coeff;
    }

    public void setCoeff(int coeff)
    {
        this.coeff = coeff;
    }

    public void setVar(char var)
    {
        this.var = var;
    }

    public void setPow(int pow)
    {
        this.pow = pow;
    }

    public String getMono(String monomials)
    {
        return mono;
    }

    // Method to further analyse user's input.
    public void analyse(String polynomial)
    {
        //Split the poynomial into monomials and store them in an array list.
        polynomial = polynomial.replaceAll("-","+-");
        String polyParts[] = polynomial.split("\\+");
        ArrayList<String> monomials = new ArrayList<String>(Arrays.asList(polyParts));  

        // Iterate the monomials.
        for (int i = 0; i <= monomials.size(); i++)
        {
            String monomial = monomials.get(i);

            // Analyse the monomial.
            for (int x = 0; x <= monomial.length(); x++)
            {
                char c = monomial.charAt(x);
                int countcoeff = 0;
                int countvar = 0;

                // check if negative.
                if (c == minus)
                {
                    isnegative = true;
                    x++;
                }
                // get the coefficient.
                if (Character.isDigit(c))
                {
                    while (Character.isDigit(c))
                    {
                        countcoeff++;
                        x++;
                    }
                    if (isnegative)
                    {
                        setCoeff(Integer.parseInt(monomial.substring(1, countcoeff)));
                    } else
                    {
                        setCoeff(Integer.parseInt(monomial.substring(0, countcoeff)));
                    }
                }
                // get the variable.
                if (Character.isLetter(c))
                {
                    char var = c;
                    while (Character.isLetter(var)) 
                    {
                        countvar++;
                        addpow++;
                        x++;
                    }
                }
                // get the power.
                if (c == powsign)
                {   
                    countvar++;
                    x++;        
                    while (Character.isDigit(c))
                    {
                        x++;
                    }
                    if (isnegative)
                    {
                        setPow(Integer.parseInt(monomial.substring(countcoeff+countvar+2, x)));
                    } else
                    {
                        setPow(Integer.parseInt(monomial.substring(countcoeff+countvar+1, x)));
                    }
                    pow += addpow;
                }
            }

            if (isnegative)
            {
                stringBuilder.append(String.valueOf(minus));
            }
            stringBuilder.append(String.valueOf(coeff) + String.valueOf(var) + String.valueOf(powsign) + String.valueOf(pow));
            mono = stringBuilder.toString();
            monomials.set(i, mono);
        }

        for (int i = 0; i < monomials.size(); i++)
        {
            System.out.println(String.valueOf(monomials.get(i)));
        }
    } // End of method analyse().

} // End of class Monomial

共 (3) 个答案

  1. # 1 楼答案

    你的一个循环正在无限地运行。你应该用if条件替换它

    while (Character.isDigit(c))
                    {
                        countcoeff++;
                        x++;
                    }
    

    换成

    if (Character.isDigit(c))
                    {
                        countcoeff++;
                        x++;
                    }
    

    或者你可以在这里使用break语句

  2. # 2 楼答案

    正如其他人已经说过的那样

    while (Character.isDigit(c))
    

    这是你的问题。 但是你有两次而不是一次,所以两次都是问题。第二个不是真正的问题,因为Character.isDigitif (c == powsign)不能同时都是真的,所以第二个infit循环永远不会执行,这就把我带到了下一个问题:bug
    在你的代码中有大量这样的代码:-D
    两个for循环都运行到远(<= .size() & <= .length()),replace<;=使用<
    此外,代码中放置的x++也是错误的。x会自动递增,如果您想提前退出循环,请使用break;或者如果您想提前跳转到下一个迭代,请使用continue;

  3. # 3 楼答案

    你有几个永远不会退出的循环:

    while (Character.isDigit(c))
    {
        countcoeff++;
        x++;
    }
    

    如何找到这样的东西? 如果使用Eclipse,可以在调试模式下运行代码,切换到调试透视图并单击黄色的暂停符号。这将暂停您的程序,在调试视图中,您可以看到线程“挂起”在哪一行,如果您单击它,它将打开源代码

    如果不使用带有该功能的IDE,可以使用JDK工具:使用jps查找程序的ID:

    C:\jdk\jdk8u45x64\jdk1.8.0_45\bin>jps
    7216
    5688 Jps
    6248 Monomials
    

    然后使用jstack打印所有运行线程的堆栈跟踪:

    C:\jdk\jdk8u45x64\jdk1.8.0_45\bin>jstack 6248
    [other threads omitted]
    
    "main" #1 prio=5 os_prio=0 tid=0x000000000203e800 nid=0x1b2c runnable [0x000000000201e000]
       java.lang.Thread.State: RUNNABLE
            at Monomials.analyse(Monomials.java:77)
            at Monomials.main(Monomials.java:10)