2015-12-13 69 views
2

我想完成一个练习,其中我有一些任务包括这一个: 仅打印数组中的3的倍数 我必须使用小程序,而且我不知道如何去做。 我试图设置上的图形部分的条件,但它在另一种尝试中,我试图基于对图3中,值数倍的数的计算来创建一个新的数组返回一个不是很好0仅打印数组中的3的倍数

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

程序无法启动,我得到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); 
    } 
} 

我该如何解决这个问题?

+0

提示:错误消息'ArrayIndexOutOfBoundException'意味着你试图遍历一个指数超越数组的长度。问题在于: 'for(int i = 0; i femmestem

+1

不,这不是问题。它会迭代,因为它检查__lesser比___ - 不__lesser或equal__ –

+0

编译器在这里吐出火:multiple3 [i] = dataList [i]; 我试着按照Thisaru Guruge的建议,没有运气。 – JohnSnow

回答

0

我自己解决了问题,只改变了图形部分的代码。 只有丑陋的事情是,我不得不重复数据列表,因为我无法将值从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); 
    } 
    } 
} 
+1

我回答了你问的问题 - __Print只有数组中的3的倍数_ 我们无法知道你的想法是什么。如果您在3的倍数发生时询问如何调整您的位置,我们将回答这个问题。 –

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

还是最好的方法是使用ListArrayList)增加的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

+0

我尝试了两种方法,但他们不工作。第二种方法是给ArayIndexOutOfBoundsException – JohnSnow

+0

你是否正确地初始化了'mul3counter'?看看答案。我更新了它! –

+0

第二种方法不能有'ArrayIndexOutOfBoundsException'。 'ArrayList'没有边界。 –