1

我试图在一个子区间上输入一个 for 循环,该子区间在指定范围内形成另一个分区,以便计算给定多项式的根。我的问题是我的“if”和“if-else”没有被执行,即使条件应该确保它们的执行。我在程序的其他部分测试了我的“poly”函数,没有发现任何问题。这是我麻烦的for循环。

for (int i = L; i < R; i += resolution) {
       double a = i, b = resolution+i;
       { if (poly(C, a)*poly(C, b) < 0) {
          double mid = findRoot(C, a, b, tolerance);
          System.out.println(mid);
          if (Math.abs(poly(C, mid)) < threshold){
            System.out.println("Root found at: "+mid);
            numCount = 1;
          }
       } else if (poly(D, a)*poly(D, b) < 0) {
          double mid = findRoot(D, a, b, tolerance);
          if(Math.abs(poly(C, mid)) < threshold) {
            System.out.println("Root found at: "+mid);
            numCount = 2;

       } else {
         numCount = 3;
         System.out.println("No roots were found in the specified range.");
       } System.out.println("numCount is "+numCount); break;
     }
  }
4

2 回答 2

0

您想要删除紧接在单词“if”之前开始的一组括号。

于 2015-05-10T18:03:06.847 回答
0

您有一组多余的括号应该被删除。这些括号使else ifandelse子句适用于 inner if,这是不正确的:

for (int i = L; i < R; i += resolution) {
   double a = i, b = resolution+i;
   if (poly(C, a)*poly(C, b) < 0) {
      double mid = findRoot(C, a, b, tolerance);
      System.out.println(mid);
      if (Math.abs(poly(C, mid)) < threshold){
        System.out.println("Root found at: "+mid);
        numCount = 1;
      }
   } else if (poly(D, a)*poly(D, b) < 0) {
      double mid = findRoot(D, a, b, tolerance);
      if(Math.abs(poly(C, mid)) < threshold) {
        System.out.println("Root found at: "+mid);
        numCount = 2;

   } else {
     numCount = 3;
     System.out.println("No roots were found in the specified range.");
   } 
   System.out.println("numCount is "+numCount); break;
}
于 2015-05-10T18:05:24.580 回答