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

发布于 2024-11-29 12:23:55 字数 1468 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雾里花 2024-12-06 12:23:55

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

更新:现在我看到了许多看起来像问题的事情,因为我看到了您正在尝试做的事情。除非我弄错了,否则你的输入文件的第一行有一个名称,后面跟着 70 行,每行都有一个字母?

一方面,当读取一封信并将其发送到 countNum() 时,您只有一个名为“n”的变量,无论您看到 A 还是 B,您都会递增该变量,然后将“n”添加到 A 和 B。这并不是将 A 的数量或 B 的数量加一。它总是会为两者加一。

接下来,由于您只向 countNum() 发送一个字母,因此外部 for 循环将只执行一次。这意味着您只能将值放入 a[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() {...}
}

Your logic is very difficult to follow with all the a, b, +1, -1, /2, etc. You should try to reduce it to the smallest amount of code that demonstrates your problem. Odds are that you'll find the problem while you're doing that. You might also provide some sample input. Without one or both of these, it's very difficult to help with your problem.

Update: I'm seeing a number of things that look like problems now that I see what you're trying to do. Unless I'm mistaken, your input file has a name on the first line followed by 70 lines, each with a single letter on them?

For one thing, when read a letter and send it into countNum(), you only have one variable called "n" that you increment whether you see an A or a B, and then you add "n" to both A and B. That's not adding one to either the number of A's or the number of B's. It will always add one to both of them.

Next, since you only send a single letter into countNum(), the outer for loop will only execute one time. That means you'll only ever put a value into a[0] and b[0]. Further, because of the "n" problem, both values will always be 1. Thus the one time you get to the inner for loop, it will always print "1A-1B, 0A-0B, 0A-0B, 0A-0B" for the first part of the answer.

After that, it should be obvious why your percentage() method doesn't work. All but the first position of the array have zeroes in them.

Additional stuff: You define a constant "dimen" equal to 4 but you sometimes use the constant and sometimes use a literal "4". Pick one and stick with it. I'd recommend the constant, and I'd recommend naming it something meaningful, like "NUMBER_OF_PERSONALITY_DIMENSIONS", if that's what it is. For that matter, give all of your variables better names, and it will make things easier for you and me both. Also, in your type() method, you say for ( int i = 0; i <= dimen; i++ ) { to iterate over an array which I think only has 4 elements. That's going to break. Finally, as you kind of mentioned elsewhere, don't pass arrays around, mutating them in multiple different methods. That's a good way to get lost. In general, make methods non-side-effecty. Instead of modifying the things you pass to them, return the important values from the method.

In general, I think you need to take a break and straighten out in your head what you're trying to do. The logic doesn't seem to make any sense.

I don't know if you've learned about this kind of thing yet, but you might consider splitting this into two classes: one to read in the data and one to tally it up. The tallying one might look something like:

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

    public void gotNewA() {...}
    public void gotNewB() {...}
}
谎言 2024-12-06 12:23:55

你除以零,问题就在于此。在上述行中,您有:

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

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

You are dividing by zero, the problem is that. On the mentioned line, you have:

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

Sometimes, a[i]+b[i] is zero. Maybe the problem will be solved by a check like this:

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

But logically, you should not divide a number by zero. Then maybe you have to correct your algorithm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文