我收到语法错误而且我不知道为什么

时间:2013-03-01 00:16:06

标签: java

我在这个读取方法的第103行遇到了语法错误,我不知道为什么。让我知道你在想什么。错误是因为 else ,它只是告诉我删除else,这不是一个可行的解决方案

    public void read()
{
    int a = 0;
    int b = 0;
    int c = 0;

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter a quadratic equation in standard format.");
    String formula = keyboard.next();

    int x = formula.indexOf('x');

    if (x < 0)
    {
        c = Integer.parseInt(formula);
    }

    else
    {
        String aa; 
        int x2 = formula.lastIndexOf('x');
        if (x == x2)
        {
            if ((x+1) != formula.length())
            {
                aa = formula.substring(x, (x+3));
            }
            else
            {
                aa = formula;
            }

            if(aa.equalsIgnoreCase("x^2"))
            {
                String aa2 = formula.substring(0, x+3);
                String aaa = formula.substring(0, x);
                if (aa2.equalsIgnoreCase("x^2"))
                        a = 1;
                else
                {
                    a = Integer.parseInt(aaa);
                }
                String cc = formula.substring(x + 3);
                if (cc.equals(""))
                {
                    c = 0;
                }
                else
                {
                    c = Integer.parseInt(cc);
                }
            }
            else
            {
                String bbb = formula.substring(0, x);
                if (x == 0)
                {
                    b = 1;
                }
                else
                {
                    b = Integer.parseInt(bbb);
                }
                String cc = formula.substring(x + 1);
                System.out.println("c = " + cc);
                    if(cc.equals(""))
                    {
                        c = 0;
                    }
                    else
                    {
                        c = Integer.parseInt(cc);
                    }

            }
        }

        else
        {
            String aa2 = formula.substring(0, x +3);
            String aaa = formula.substring(0, x);
                if (aaa.equalsIgnoreCase("x^2"))
                {
                    a = 1;
                }
                else
                {
                    a = Integer.parseInt(aa2);
                }
                    if((x2 + 1) != formula.length())
                    {
                        if(formula.charAt(x2 + 1) == '+');
                        {
                            String cc2 = formula.substring(x2 + 2);
                            c = Integer.parseInt(cc2);
                        }}
                        else if (formula.charAt(x2 + 1) == '-')
                        {
                            String cc2 = formula.substring(x2 + 1); 
                            c = Integer.parseInt(cc2);
                        }

                    }
                **else**
                    {
                        c = 0;

                        if(formula.charAt(x + 3) == '+')
                        {
                            String bb2 = formula.substring((x + 4), x2);
                            b = Integer.parseInt(bb2);
                        }
                        else
                        {
                            String bb2 = formula.substring((x + 3), x2);
                            b = Integer.parseInt(bb2);
                        }

            }
        }

    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);

    }

2 个答案:

答案 0 :(得分:1)

                    if(formula.charAt(x2 + 1) == '+');
                    {
                        String cc2 = formula.substring(x2 + 2);
                        c = Integer.parseInt(cc2);
                    }}

if语句后面有一个分号,并用两个括号关闭。

答案 1 :(得分:0)

你有一个额外的}个字符......

if((x2 + 1) != formula.length())
                    {
                        if(formula.charAt(x2 + 1) == '+');
                        {
                            String cc2 = formula.substring(x2 + 2);
                            c = Integer.parseInt(cc2);
                        }} <<<<<<