2011-08-13 44 views
2

我不断收到在线程异常“主”:如何解决线程“main”,java.lang.ArithmeticException中的异常:/ by zero?

java.lang.ArithmeticException:/by zero 
at PersonalityTest.percentage(PersonalityTest.java:85) 
at PersonalityTest.main(PersonalityTest.java:19) 

我想每次扫描器读取A或B的时间加计数,并得到B.

的百分比
import java.io.*; 
import java.util.*; 

public class PersonalityTest { 

    public static final int dimen = 4; 

    public static void main(String [] args) throws FileNotFoundException { 
     Scanner input = new Scanner(new File("personality.txt")); 
     PrintStream out = new PrintStream(new File("output.txt")); 

     int[] a = new int[4]; 
     int[] b = new int[4]; 

     welcome(); 

     while (input.hasNextLine()) { 
      String letter = letter(input, out); 
      countNum(a, b, out, letter); 
      int[] percentage = percentage(a, b, out); 
      type(out, percentage); 
      out.println(""); 
     } 
    } 

    public static void welcome() { 
     System.out.println("The Keirsey Temperament Sorter is a test that measures four independent dimensions of your personality: "); 
     System.out.println("1. Extrovert versus Introvert (E vs. I): what energizes you"); 
     System.out.println("2. Sensation versus iNtuition (S vs. N): what you focus on"); 
     System.out.println("3. Thinking versus Feeling (T vs. F): how you interpret what you focus on"); 
     System.out.println("4. Judging versus Perceiving (J vs. P): how you approach life"); 
     System.out.println(""); 
    } 


    public static String letter (Scanner input, PrintStream out) { 
     String name = input.nextLine(); 
     out.println(name + ":"); 
     String letter = input.nextLine(); 
     return letter; 
    } 

    public static void countNum(int[] a, int[] b, PrintStream out, String letter) { 
     int[] countA = new int[7]; 
     int[] countB = new int[7]; 
     int n = 0; 

     out.print("answers: ["); 

     for (int i = 0; i < letter.length(); i++) { 
      int type = i % 7; 

      if (letter.charAt(i) == 'A' || letter.charAt(i) == 'a') { 
       n = 1; 
      } 
      else if (letter.charAt(i) == 'B' || letter.charAt(i) == 'b') { 
       n = 1; 
      } 
      countA[type] += n; 
      countB[type] += n; 

      if (type == 2 || type == 4 || type == 6) { 
       a[type/2] = countA[type - 1]+ countA[type]; 
       b[type/2] = countB[type - 1]+ countA[type]; 
      } else if (type == 0) { 
       a[0] = countA[0]; 
       b[0] = countB[0]; 
      } 
      for (int j = 0; j < dimen; j++) { 
       out.print(a[j] + "A-" + b[j] + "B," + " "); 
      } 
     } 
     out.print("]"); 
    } 

    public static int[] percentage (int[] a, int[] b, PrintStream out) { 
     int[] percentage = new int [4]; 
     out.print("percent B: ["); 
     double n = 0.0; 

     for (int i = 0; i < dimen; i++) { 
      n = b[i] * 100/(a[i] + b[i]); // <--- This is where I get error 
      percentage [i] = (int) Math.round(n); 
      out.print(percentage[i]); 
     } 

     out.print("]"); 
     return percentage; 
    } 

    public static void type (PrintStream out, int[] percentage) { 
     String[] type = new String [4]; 

     out.print("type: "); 

     for (int i = 0; i <= dimen; i++) { 
      while (percentage[i] > 50) { 
       if (i == 0) { 
        type[1] = "I"; 
       } 
       else if (i == 1) { 
        type[2] = "N"; 
       } 
       else if (i == 2) { 
        type[3] = "F"; 
       } 
       else { 
        type[4] = "P"; 
       } 
      } 

      while (percentage[i] < 50) { 
       if (i == 0) { 
        type[1] = "E"; 
       } 
       else if (i == 1) { 
        type[2] = "S"; 
       } 
       else if (i == 2) { 
        type[3] = "T"; 
       } 
       else { 
        type[4] = "J"; 
       } 
      } 
      out.print(Arrays.toString(type)); 
     } 
    } 
} 
+17

不要在PersonalityTest.java的第85行除以零。精明? –

回答

10

你逻辑非常难以遵循所有的a,b,+1,-1,/ 2等。您应该尝试将其减少到显示您的问题的最小代码量。可能性是,当你这样做时你会发现问题。您也可以提供一些示例输入。如果没有其中一个或两个,就很难帮助解决您的问题。

更新:我看到一些看起来像问题的事情,现在我看到你正在尝试做什么。除非我错了,你的输入文件在第一行上有一个名字,后面跟着70行,每行都有一个字母?

首先,当读取一个字母并将其发送到countNum()时,您只有一个名为“n”的变量,无论您看到A还是B,然后将“n”添加到两个A和B.这不是对A的数量或B的数量加一。它会一直为它们添加一个。

接下来,由于您只发送一个字母到countNum(),外部for循环只会执行一次。这意味着你只会将一个值放入[0]和b [0]中。此外,由于“n”问题,两个值都将始终为1.因此,一旦进入内循环,它将始终打印“1A-1B,0A-0B,0A-0B,0A-0B”为答案的第一部分。

之后,它应该很明显,为什么你的百分比()方法不起作用。除数组的第一个位置外,其他所有位置均为零。其他内容:您定义了一个等于4的常量“dimen”,但您有时使用常量,有时使用文字“4”。挑一个并坚持下去。我建议常量,并且我建议将它命名为有意义的,比如“NUMBER_OF_PERSONALITY_DIMENSIONS”,如果是这样的话。对于这个问题,给你所有的变量更好的名字,它会让你和我都更容易。另外,在你的type()方法中,你说for (int i = 0; i <= dimen; i++) {迭代一个我认为只有4个元素的数组。这将打破。最后,正如你在其他地方提到的那样,不要传递数组,而是用多种不同的方法来改变它们。这是迷路的好方法。一般来说,使方法非副作用。不要修改传递给它们的东西,而是返回方法中的重要值。

总的来说,我认为你需要休息一下,理清你想要做的事情。这个逻辑似乎没有任何意义。

我不知道你是否已经知道这种事情,但你可以考虑将它分成两类:一类用于读取数据,另一类用于记录数据。理货人可能看起来像这样:

class Tallier { 
    private int numberOfAs; 
    private int numberOfBs; 
    private int totalEntriesSoFar; 
    private int[] typeATotals; 
    private int[] typeBTotals; 

    public void gotNewA() {...} 
    public void gotNewB() {...} 
} 
+0

要求是从原来的70个A/B答案到As和Bs的计数,按维度分组;从计数到Bs的百分比的 ;和 从百分比到四字母个性字符串。 – user892718

3

你被除以零,问题是这样的。在上述行中,您有:

n = b[ i ] * 100/(a[ i ] + b[ i ]); 

有时,a [i] + b [i]为零。也许这个问题将通过检查这样解决:

for(int i = 0; i < dimen; i++) { 
     if (a[ i ] + b[ i ]!= 0) 
      n = b[ i ] * 100/(a[ i ] + b[ i ]); 
     else 
      //assign a number to n for this situation 
     percentage [ i ] = (int) Math.round(n); 
     out.print(percentage[ i ]); 
} 

但是从逻辑上讲,你不应该除以零的数字。那么也许你必须纠正你的算法。

+0

如果你看看我的答案,他的问题就会比只是偶尔让'a [i] + b [i]'解决为零。实际上,如果我正确理解了程序的目的,'a [i] + b [i]'不应该为零。 –

相关问题