将数组中的所有元素乘以一个外部数字?

3

我需要将数组中的所有值乘以3000,从而创建一个新数组,然后用该数组来减去另一个数组。我试图创建一个单独的方法来为我完成这个任务,但是我得到的乘以后的数组只是一堆奇怪的数字和符号?

以下是我编写的代码:

public static void main(String[] args)
{    
    int numberOfTaxpayers = Integer.parseInt(JOptionPane.showInputDialog("Enter how many users you would like to calculate taxes for: ");
    int[] usernumChild = new int[numberOfTaxPayers];
    for (int i = 0; i < usernumChild.length; i++)
    {
        usernumChild[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter number of children for user "+ (i+1) +": "));
    }//this for loop finds out the number of children per user so we can later multiply each input by 3000 to create an array that determine dependency exemption for each user
int[] depndExemp = multiply(usernumChild, 3000);//this was the calling of the multiply method... somewhere here is the error!!
}//end main method 
public static int[] multiply(int[] children, int number)
{
    int array[] = new int[children.length];
    for( int i = 0; i < children.length; i++)
    {
       children[i] = children[i] * number;
    }//end for
    return array;
}//this is the method that I was shown in a previous post on how to create return an array in this the dependency exemption array but when I tested this by printing out the dependency array all I received were a jumble of wrong numbers.

“a new array that I will use to subtract from another array” 的意思是什么? - user1907906
@Lutz Horn 我还没有包括那部分,但我需要做的是获取依赖数组(为每个输入将子项乘以3000),然后从另一个包含每个用户总收入的数组中减去,这会导致在净收入下创建另一个数组...由于某种原因,我们的讲师想要每种数据类型都有一个数组。 - prodo
6个回答

5

在你的例子中,你正在将children数组乘以新数组,但返回的是新数组。你需要将新数组乘以children数组。

1 public static int[] multiply(int[] children, int number)
2 {
3     int array[] = new int[children.length];
4     for( int i = 0; i < children.length; i++)
5     {
6         array[i] = children[i] * number;
7     }//end for
8     return array;
9 }

你看到奇怪的符号是因为返回了未初始化的值。数组本身在第3行分配,但此时数组的每个索引都没有被初始化,因此我们不知道里面有什么值。


我应该在哪里初始化返回值?我需要最初更改第二个方法吗? - prodo
我会为您澄清答案。 - Tyler
好的,我现在明白了,谢谢。但是我该如何将usernumChild的内容传递到另一个方法中呢?我以为在multiply(usernumChild, 3000)中声明它就可以了?我能否只在主函数中执行算法而不必传递数组,从而摆脱第二个方法?目前简单性很重要。 - prodo
你是正确的,你应该在一个循环中完成所有操作。由于你可以一次性完成所有工作,所以你不需要为每个操作迭代多次循环。如果你不是为了效率而是为了易读性,你可以将每个操作分解成自己的方法,但我不建议这样做。 - Tyler

3
使用Java 8流,它可以非常简单: ```` 使用Java 8流,这个过程非常简单: ````
public static int[] multiply(int[] children, int number) {

    return Arrays.stream(children).map(i -> i*number).toArray();

}

2

你实际上不需要在方法中创建新的数组(而且你也返回了旧的数组,没有任何改变)。所以只需执行以下操作:

public static int[] multiply(int[] children, int number) {
    for(int i = 0; i < children.length; i++) {
        children[i] = children[i] * number;
    }
    return children;
}

我能否不创建新的方法进行乘法运算?我想尽可能保持这个程序的简洁和清晰。目前还有36小时的工作时间,需要完成更多的内容才能达到预期的结果,看起来情况相当糟糕! - prodo
是的,你可以这样做,但最好有多个方法而不是一个巨大的方法,因此这是正确的设计。经验法则是每个方法都应该适合屏幕。 - Petr Mensik

1
您需要更改。
children[i] = children[i] * number;

 array[i] = children[i] * number;

1
如果我理解你的问题正确:
children[i] = children[i] * number;

应该改为:

应该被更改为

array[i] = children[i] * number;

考虑到您返回的是array而不是children

0
在你的第二个for循环中应该是:
for(int i = 0; i < children.length; i++){
       array[i] = children[i] * number;
}//end for

同时确保所有children[i]的值都小于((2^31 - 1)/number) +1


嗯,我尝试过了,但仍然得到了一个 [I@4a3a6e5c 的 system.print。 - prodo
@prodo 这意味着您正在打印所创建的 int 数组的引用。要打印数组,您可以使用 for 循环并打印每个值,或者使用 System.out.println(Arrays.toString(myArray)); - Alexis C.
我需要走创建一个循环的路线,所以你建议创建一个扩展的for循环吗?将depndExemp中的值放入for(int reprint : depndExemp){ System.out.println(reprint)}这个代码块中。 - prodo
如果它是一个一维数组,那么是的。 - Alexis C.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接