0

我对 java 比较陌生,所以如果我在这里遗漏了一些明显的东西,我不会感到惊讶。无论如何,我编写了一个使用二分法找到多项式根的代码。我认为该程序一切都很好而且很花哨,直到我将它从记事本++粘贴到命令提示符下,在使用 javac 编译它后,我最终得到了一堆“类、接口或枚举预期”的错误。代码本身的一切似乎都很好,所以我推断我犯了以下两个错误之一:在我复制并粘贴到命令提示符时发生了错误,或者我确实在我的代码中创建了一个错误我没抓到。有人能告诉我我做错了什么吗?这可能是一个小修复,但我只是不知道如何更改它以使我的代码正常工作。这是代码:

import java.util.*;

class Roots {

  public static int degree;
  public static double[] coArrayC;
  public static double[] coArrayD;
  public static int coeffVal;

  public static void main( String[] args ){
        double resolution = 0.01;
        double tolerance = 0.0000001;
        double threshold = 0.001;

        double rightEndPt;
        double leftEndPt;
        int polyRootPointer = 0;
        int diffRootPointer = 0;

        boolean rootAns = false;

        Scanner sc = new Scanner(System.in);


        System.out.println();

        System.out.print("Enter the degree: "); //prompts user to enter the correct degree of the polynomial
        degree = sc.nextInt();

        coeffVal = degree + 1; //the coefficient is one more than the number of degrees
        System.out.print("Enter " + coeffVal + " coefficients: "); //adds in the value of the polynomial coefficient in to the line that prompts the user to specify which coefficients are in the function
        double[] coefficients = new double[coeffVal]; //initialization of array, a bunch of doubles that represent the coefficients of the user's polynomial
        coArrayC = new double[coeffVal]; //naming the array

        double[] rootArray = new double[degree];//another array for the degrees of the polynomial

        coArrayD = new double[coeffVal]; //and assigning it a name

        for(int i = 0; i < coeffVal; i++) {
              coefficients[i] = sc.nextDouble();
        }
        System.out.print("Enter the right and left endpoints, in that order: "); //prompts user to enter the interval limits
        rightEndPt = sc.nextDouble();
        leftEndPt = sc.nextDouble();


        diff(coefficients); //calculates coefficients of derivative polynomial

        for (double i = leftEndPt; i < rightEndPt-resolution; i = i + resolution){ //
              if (isPositive(coArrayD, i) != isPositive(coArrayD, i+resolution) || isPositive(coArrayD, i) == 0) {
                    rootArrayDeriv[diffRootPointer] = findRoot(coArrayD, i, i+resolution, tolerance);
                    diffRootPointer++;
              }
        }

        for (int i = 0; i < rootArrayDeriv.length; i++) {
              double tempValue;
              tempValue = poly(coefficients, rootArrayDeriv[i]);
              tempValue = Math.abs(tempValue);
              if (tempValue < threshold) {
                    rootArray[polyRootPointer] = rootArrayDeriv[i];
                    polyRootPointer++;
                    rootAns = true;
              }
        }

        for (double i = leftEndPt; i < rightEndPt-resolution; i = i + resolution){
              if (isPositive(coefficients, i) != isPositive(coefficients, i+resolution) || isPositive(coefficients, i) == 0) {
                    rootArray[polyRootPointer] = findRoot(coefficients, i, i+resolution, tolerance);
                    polyRootPointer++;
                    rootAns = true;
              }
        }

        //Arrays.sort(rootArray); //sorts array from lowest to highest

        if (rootAns == true) {
            System.out.println("Sorry - no roots were found in the specified interval.");
                    }
              }
        } else {
            for (int i = 0; i < rootArray.length; i++) {
                if (rootArray[i] != 0.0) {
                    System.out.printf("Root found at %.5f\n :" Arrays.sort(rootArray[i])); //if roots are found, list them as an output, with five decimal places of accuracy
        }
  }

  static double poly(double[] C, double x){
        double polySum = 0;
        coArrayC[0] = C[0];
        for (int i = 1; i < coArrayC.length; i++){
              coArrayC[i] = C[i]*(Math.pow(x, i)); //multiplies each coefficient by the designated power of X
        }
        for (int i = 0; i < coArrayC.length; i++){
              polySum = polySum + coArrayC[i]; //accumulates the sum of of all the terms, after the coeff. were multiplied to their respective powers.
        }
        return(polySum);

  }

  static double[] diff(double[] C){ 
        for (int i = 0; i < degree; i++){
              coArrayD[i] = (i+1)*C[i+1]; //newly allocated array D containing coeff. of the polynomial that is the derivative of the polynomial with coeff. array C.
        }
        return(coArrayD);
  }

  static double findRoot(double[] C, double a, double b, double tolerance){ //using bisection method; similar to findRoot.java in cmps webpage.
        double root = 0.0 , residual;
        while ( Math.abs(b - a) > tolerance ) { 
              root = (a + b) / 2.0;
              residual = poly(C, root);
              if (poly(C, a) < 0 && poly(C, b) < 0) { 
                    if (residual > 0)
                          b = root;
                    else
                          a = root;                       
              } else if (poly(C, a) > 0 && poly(C, b) > 0) { 
                    if (residual > 0)
                          a = root;  //replace left endpoint
                    else
                          b = root; //replace right endpoint
              }
        }
        return(root);
  }

  static int isPositive(double[] C, double a){
        double endpointTempA;
        endpointTempA = poly(C, a);
        if (endpointTempA < 0) {
              return(1);
        } else if (endpointTempA > 0) {
              return(2);
        } else {
              return(0); 
        }
  }
}
4

1 回答 1

0

You have two } too many here:

    if (rootAns == true) {
        System.out.println("Sorry - no roots were found in the specified interval.");
                }
          }
    } else {

If you indent your code properly, it's easier to see these kinds of errors. Remove the two } that don't belong there:

    if (rootAns == true) {
        System.out.println("Sorry - no roots were found in the specified interval.");
    } else {

There's also a missing , here, and you shouldn't pass a single double to Arrays.sort, but the whole array

System.out.printf("Root found at %.5f\n :"Arrays.sort(rootArray[i]));

Should be:

System.out.printf("Root found at %.5f\n :", Arrays.sort(rootArray));

And a missing }.

Instead of writing a whole program at once and then trying to compile it, write it little by little, and compile it each time you have for example a complete method. That way you avoid getting a mountain of little errors that confuse you.

于 2015-05-10T15:06:37.647 回答