java lang arithmetic_如何解决线程“main”,java.lang.ArithmeticException中的异常:/ by zero?...

我一直在线程“main”中遇到异常:

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] + "" + 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]); //

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));

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值