如何解决线程"main"中的异常,java.lang.ArithmeticException:/由零?

use*_*718 1 java program-entry-point exception

我一直在线程"main"中得到一个异常:

java.lang.ArithmeticException: / by zero
at PersonalityTest.percentage(PersonalityTest.java:85)
at PersonalityTest.main(PersonalityTest.java:19)
Run Code Online (Sandbox Code Playgroud)

我想在每次扫描器读取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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Rya*_*art 10

你的逻辑非常难以跟随所有的a,b,+ 1,-1,/ 2等.你应该尝试将它减少到证明你的问题的最小代码量.可能的情况是,当你这样做时,你会发现问题.您可能还提供一些示例输入.没有这些中的一个或两个,很难帮助解决您的问题.

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

首先,当读取一个字母并将其发送到countNum()时,您只有一个名为"n"的变量,无论您是看到A还是B,都会增加,然后在A和B中添加"n".这不是在A的数量或B的数量上加一个.它总是会为它们添加一个.

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

之后,很明显为什么你的percentage()方法不起作用.除了阵列的第一个位置之外的所有位置都有零.

额外的东西:你定义一个等于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() {...}
}
Run Code Online (Sandbox Code Playgroud)