2014-04-04 29 views
0

我正在尝试调试我的程序。要查看哪里出错,我需要查看输出。但是,因为它无限期地使用递归,停止程序的唯一方法是重新编译擦除输出。我应该如何调试?如何用无限循环调试程序?

我打电话给DTNode。

这里是我的类:

package DecisionTree; 

public class DTNode { 

    Instance[] instances; 
    double cutoff; 
    DTNode left, right; 

    public DTNode (Instance[] instance, int l, int r) { 
     this.instances = instance; 
     int i; 
     int j = 0; 
     int k = 0; 
     int getIndex; 
     double[] cutoff = new double[instance.length]; 
     double[] entropy = new double[instance.length]; 
     int[] split = new int[instance.length]; 
     double smallestEntropy; 

     for(i=0; i<r; i++) { 
     if(instance[i].label != instance[i+1].label) { 
      cutoff[j] = (instance[i].attribute + instance[i+1].attribute)/2; 
      split[j] = i; 
      System.out.println("Cutoff is: " + cutoff[j] + ". Split is: " + split[j]); 
      j++; 
     } 
     } 

     for(k=0; k<j; k++) { 
     entropy[k] = calcEntropy(instance, l, cutoff[k], r); 
     System.out.println(entropy[k]); 
     } 

     smallestEntropy = entropy[0]; 
     getIndex = split[0]; 
     for(k=1; k<j; k++) { 
     if (entropy[k] < smallestEntropy) { 
      smallestEntropy = entropy[k]; 
      getIndex = k; 
     } 
     } 
     System.out.println(getIndex + " " + entropy[getIndex]); 
     if((r - l) == 0 || j == 0) { 
     this.left = null; 
     this.right = null; 
     } 
     else{ 
     this.left = new DTNode(instance, l, getIndex); 
     this.right = new DTNode(instance, getIndex+1, r); 
     } 
    } 

    public double calcEntropy(Instance[] inst, int a, double b, int c) { 
     int i; 
     double leftSideCounter = 0; 
     double rightSideCounter = 0; 
     double leftTrue = 0; 
     double rightTrue = 0; 
     double leftSideEntropy = 0; 
     double rightSideEntropy = 0; 
     double leftTrueFraction; 
     double leftFalseFraction; 
     double rightTrueFraction; 
     double rightFalseFraction; 
     double leftTotalFraction; 
     double rightTotalFraction; 
     double entropy; 

     for(i=a; i<=c; i++){ 
     if(inst[i].attribute < b) { 
      leftSideCounter++; 
     } 
     else { 
      rightSideCounter++; 
     } 
     } 

     for(i=0; i<leftSideCounter; i++) { 
     if(inst[i].label == true) { 
      leftTrue++; 
     } 
     } 

     for(i=(int)leftSideCounter; i<(int)(rightSideCounter+leftSideCounter); i++) { 
     if(inst[i].label == true) { 
      rightTrue++; 
     } 
     } 


     leftTrueFraction = leftTrue/leftSideCounter; 
     leftFalseFraction = (leftSideCounter-leftTrue)/leftSideCounter; 

     rightTrueFraction = rightTrue/rightSideCounter; 
     rightFalseFraction = (rightSideCounter-rightTrue)/rightSideCounter; 

     leftTotalFraction = leftSideCounter/(leftSideCounter+rightSideCounter); 
     rightTotalFraction = rightSideCounter/(leftSideCounter+rightSideCounter); 

     if(leftTrue == 0 || (leftSideCounter - leftTrue) == 0) { 
     leftSideEntropy = 0; 
     } 
     else{ 
     leftSideEntropy = -leftTrueFraction*logb2(leftTrueFraction)-leftFalseFraction*logb2(leftFalseFraction); 
     } 

     if (rightTrue == 0 || (rightSideCounter - rightTrue) == 0) { 
     rightSideEntropy = 0; 
     } 
     else{ 
     rightSideEntropy = -rightTrueFraction*logb2(rightTrueFraction)-rightFalseFraction*logb2(rightFalseFraction); 
     } 
     entropy = leftSideEntropy*leftTotalFraction+rightSideEntropy*rightTotalFraction; 

     System.out.println(leftTrue); 
     System.out.println("leftTrueFraction = " + leftTrueFraction + ". leftFalseFraction = " + leftFalseFraction); 
     System.out.println("rightTrueFraction = " + rightTrueFraction + ". rightFalseFraction = " + rightFalseFraction); 
     System.out.println("leftTotalFraction = " + leftTotalFraction + ". rightTotalFraction = " + rightTotalFraction); 
     System.out.println("leftSideEntropy = " + leftSideEntropy + ". rightSideEntropy = " + rightSideEntropy); 
     return entropy; 
    } 

    public double logb2(double b) { 
     return Math.log(b)/Math.log(2); 
    } 
} 
+4

使用调试器... – LeatherFace

+0

我不明白为什么调试器不会工作... – Kon

+0

使用调试器?用'System.out.println'语句显示变量的状态?基本上,就像你会调试其他任何东西。 – yshavit

回答

1

所有主要的IDE提供了调试功能。您可以使用这些调试器轻松调试java文件。

  1. 这是Debugging with eclipse的教程。
  2. 如何intelliJ
  3. 调试如何netbean
  4. Java本机调试器调试link here

您还可以在网上找到大量的视频。只需搜索Java调试。