【问题标题】:Print only the multiples of 3 in the array仅打印数组中 3 的倍数
【发布时间】:2015-12-13 20:55:21
【问题描述】:

我正在尝试完成一项练习,其中有一些任务,包括以下任务: 仅打印数组中 3 的倍数 我必须使用小程序,但我不知道该怎么做。 我试图在图形部分设置一个条件,但它返回一个不好的 0

public void init() {
  dataList = new int[17];
  int dataList[] = {2,4,6,9,5,4,5,7,12,15,21,32,45,5,6,7,12};

  for (int i = 0; i < dataList.length; i++) {
    //Compute the sum of the elements in the array.
    sum += dataList[i];
    //Compute the product of the elements in the array.
    product *= dataList[i];
    //Compute the frequency of the number 5 in the array
    if (dataList[i] == 5) {
      fiveCounter++;
    }
  }
}

public void paint(Graphics g) {
  g.drawString(("Sum of elements is: " + sum), 25, 25);
  g.drawString(("Product of elements is: " + product), 25, 50);
  g.drawString(("Number 5 is present " + fiveCounter + " times"), 25, 75);
  for (int i = 0; i < dataList.length; i++) {
    if ((dataList[i] % 3) == 0) {
      g.drawString((String.valueOf(dataList[i])), 25, 100);
    }
  }
}

在我尝试基于 3 的倍数的值的数量的计算创建一个新数组的另一次尝试中,程序没有启动,我得到 ArrayIndexOutOfBoundException

public void init() {
  dataList = new int[17];
  multiple3 = new int[mult3Counter];
  int dataList[] = {2,4,6,9,5,4,5,7,12,15,21,32,45,5,6,7,12};

  for (int i = 0; i < dataList.length; i++) {
    //Compute the sum of the elements in the array.
    sum += dataList[i];
    //Compute the product of the elements in the array.
    product *= dataList[i];
    //Compute the frequency of the number 5 in the array
    if (dataList[i] == 5) {
      fiveCounter++;
    }
    if ((dataList[i] % 3) == 0) {
      multiple3[i] = dataList[i];
      mult3Counter++;
    }
  }

public void paint(Graphics g) {
  g.drawString(("Sum of elements is: " + sum), 25, 25);
  g.drawString(("Product of elements is: " + product), 25, 50);
  g.drawString(("Number 5 is present " + fiveCounter + " times"), 25, 75);
  for (int i = 0; i < multiple3.length; i++) {
    g.drawString((String.valueOf(multiple3[i])), 25, 100);
  }
}

我该如何解决这个问题?

【问题讨论】:

  • 提示:错误消息ArrayIndexOutOfBoundException 表示您尝试迭代超出数组长度的索引。问题出在这里:for (int i = 0; i &lt; dataList.length; i++)for (int i = 0; i &lt; multiple3.length; i++) 由于数组的第一个索引为零,您实际上想要遍历 array.length - 1
  • 没有。这不是问题。它将在检查 小于 - 不是 小于或等于 时进行迭代
  • 编译器在这里喷火:multiple3[i] = dataList[i];我按照 Thisaru Guruge 的建议尝试了,但没有成功。
  • 正如我在回答中提到的,您必须使用两个不同的计数器。 multiple3[i] = dataList[i]; 将不起作用。 multiple3[j] = dataList[i]; 会做
  • 编译成功,但不显示3的倍数的输入,执行时抛出异常错误。

标签: java arrays algorithm indexoutofboundsexception


【解决方案1】:

您不能对两个数组使用相同的计数器。使用两个不同的计数器。您不能使用mult3Counter 作为数组multiple3 的大小,因为它未初始化!因此,mult3Counter 默认为0。因此,当您要使用任何索引访问multiple3[] 时,它将给出ArayIndexOutOfBoundsException

如果你需要知道3的倍数出现的次数,你必须运行两次循环;

int mult3Counter = 0;
for (int i = 0; i < dataList.length; i++) {
  if ((dataList[i] % 3) == 0) {
    mult3Counter++;
  }
}

int j = 0;
int [] multiple3 = new int[mult3Counter];

for (i = 0; i < dataList.length; i++)
{
  if ((dataList[i] % 3) == 0)
  {
    multiple3[j++] = dataList[i];
  }
}

或者最好的方法是使用List (ArrayList) 将3的倍数相加。

ArrayList<int> multiple3 = new ArrayList<>();

for (int i = 0; i < dataList.length; i++) {
  if ((dataList[i] % 3) == 0) {
    multiple3.add(dataList[i]);
  }
}

如果你需要一个数组,你可以稍后将它转换成一个数组。参考This Question

【讨论】:

  • 我尝试了两种方法,但它们都不起作用。第二种方法是给 ArayIndexOutOfBoundsException
  • 您是否正确初始化了mul3counter?看答案。我更新了!
  • 第二种方法不能有ArrayIndexOutOfBoundsExceptionArrayLists 没有界限。
【解决方案2】:

我自己解决了问题,仅更改了图形部分中的代码。 唯一难看的是我不得不重复 dataList,因为我无法从 init 方法中传输值:

public void paint(Graphics g) {
  int dataList[] = {2,4,6,9,5,4,5,7,12,15,21,32,45,5,6,7,12};
  int yposition = 100;

  g.drawString(("Sum of elements is: " + sum), 25, 25);
  g.drawString(("Product of elements is: " + product), 25, 50);
  g.drawString(("Number 5 is present " + fiveCounter + " times"), 25, 75);
  g.drawString(("The following numbers are multiple of 3"), 25, yposition);

  for (int i = 0; i < dataList.length; i++) {
    if ((dataList[i] % 3) == 0) {
      yposition += 15;
      g.drawString((String.valueOf(dataList[i])), 25, yposition);
    }
  }
}

【讨论】:

  • 我回答了您提出的问题 - 仅打印数组中 3 的倍数 我们无法知道您的想法是对的。如果您问如何在 3 的倍数出现时调整您的位置,我们会回答。
猜你喜欢
  • 2020-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
相关资源
最近更新 更多