使用代码获取错误

时间:2016-12-12 20:19:14

标签: java compiler-errors

我在"公共多项式乘法(多项式p)"和"公共多项式加(多项式p)"。我不知道为什么会这样。我没有看到任何问题。这是代码,我将把错误放在底部。

 import java.util.*;
    class Term
    {
        private int coe;
        private int pow;
        public int getCoe()
        {return coe;}
        public int getPow()
        {return pow;}
        public void setCoe(int coe)
        {this.coe=coe;}
        public void setPow(int pow)
        {this.pow=pow;}
        public Term(int coe, int pow)
        {
            this.coe=coe;
            this.pow=pow;
        }
        public String toString()
        {
            return "(" +coe+ "," +pow+ ")";
        }
    }
    class Polynomial
    {
        private LinkedList poly;
        public Polynomial()
        {
            poly = new LinkedList();
        }
        public void addTerm(int coe, int pow)
        {
            Term t = new term(coe,pow);
            poly.addLast(t);
        }
        public void print()
        {
            ListIterator iter = poly.listIterator();
            while(iter.hasNext())
            {
                term temp = (Term) iter.next();
                System.out.print(temp);
            }
            System.out.println();
        }
        public void insert(int coe, int pow)
        {
            Term temp = new Term(coe,pow);
            ListIterator iter = poly.listIterator();

            while(iter.hasNext())
            {
                Term t = (Term)iter.next();
                if(t.getPow() == temp.getPow())
                {
                    t.setCoe(t.getCoe()+temp.getCoe());
                    return;
                }
                else if(t.getPow() < temp.getPow())
                {
                    iter.previous();
                    iter.add(temp);
                    return;
                }
                iter.add(temp);
            }
            public Polynomial add(Polynomial p)
            {
                Polynomial a = new Polynomial();
                ListIterator iter = poly.listIterator();
                ListIterator pIter = p.poly.listIterator();

                while(iter.hasNext())
                {
                    Term t =(Term)pIter.next();
                    a.insert(t.getCoe(),t.getPow());
                }
                return a;
            }
            public Polynomial multiply(Polynomial p)
            {
                Polynomial m = new Polynomial();
                ListIterator iter = poly.listIterator();

                while (iter.hasNext())
                {
                    Term t = (Term)iter.next();
                    ListIterator pIter = (p.poly).listIterator();
                    while (pIter.hasNext())
                    {
                        Term temp = (Term)pIter.next();
                        m.insert(t.getCoe() * temp.getCoe(), t.getPow() + temp.getPow());
                    }
                }
                return m;
            }
        }
        public class PolynomialTest
        {
            public static void main (String [] args)
            {
                Polynomial p = new Polynomial();
                p.addTerm(-10,0);
                p.addTerm(-1,1);
                p.addTerm(9,7);
                p.addTerm(5,10);

                Polynomial p2;
                p2 = p.multiply(p);
                p2.print();
            }
        }

这些是我得到的错误。

PolynomialTest.java:67: error: illegal start of expression
        public Polynomial add(Polynomial p)
        ^
PolynomialTest.java:67: error: ';' expected
        public Polynomial add(Polynomial p)
                             ^
PolynomialTest.java:67: error: ';' expected
        public Polynomial add(Polynomial p)
                                          ^
PolynomialTest.java:80: error: illegal start of expression
        public Polynomial multiply(Polynomial p)
        ^
PolynomialTest.java:80: error: ';' expected
        public Polynomial multiply(Polynomial p)
                                  ^
PolynomialTest.java:80: error: ';' expected
        public Polynomial multiply(Polynomial p)
                                               ^
PolynomialTest.java:112: error: reached end of file while parsing
    }
     ^
7 errors

我很高兴知道我做错了什么以及如何解决它,谢谢。

2 个答案:

答案 0 :(得分:1)

小心你的牙套。你希望它看起来更像这样:

public insert {
  // do stuff
} // <- end of insert method

public add {
  // do add stuff
}

public multiply {
  // do multiply stuff
}

答案 1 :(得分:0)

你缺少几个括号,就是这样。您已在Retries Attempted函数内声明了addmultiply个函数。

这是你的代码修复。

insert