【问题标题】:multiplying all elements in an array by an outside number?将数组中的所有元素乘以外部数字?
【发布时间】:2013-11-05 16:15:56
【问题描述】:

我需要将数组中的所有值乘以 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.

【问题讨论】:

  • “我将用来从另一个数组中减去的新数组”是什么意思?
  • @Lutz Horn 我还没有包含那个部分,但是我要做的是获取依赖数组(每个输入将孩子乘以 3000),然后从另一个包含每个用户的总收入,这将导致在净收入下创建另一个数组......出于某种原因,我们的讲师想要每个数据类型的数组

标签: java arrays methods


【解决方案1】:

在你的第二个 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 这意味着您正在打印您创建的 int 数组的引用。要打印您的数组,您可以创建一个 for 循环并打印每个值或使用 System.out.println(Arrays.toString(myArray));
  • 我将不得不走创建循环的路线,所以您建议创建一个扩展的 for 循环?将 depndExemp 的值放入 for(int reprint : depndExemp){ System.out.println(reprint)}
  • @prodo 如果是一维数组,是的
【解决方案2】:

你需要改变

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

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

【讨论】:

    【解决方案3】:

    您真的不必在您的方法中创建新数组(并且您还返回旧数组而不做任何更改)。所以就做吧

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

    【讨论】:

    • 我可以在不创建新方法的情况下进行乘法吗?我想尽量让这个程序保持干净和简单。在这一点上,还有 36 小时的工作时间,而且要完成最终结果还需要更多时间,至少可以说看起来很黯淡!
    • 是的,你可以,但最好有多种方法而不是一个巨大的方法,所以这是一个正确的设计。经验法则是每个方法都应该适合屏幕
    【解决方案4】:

    如果我正确理解您的问题:

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

    应该改为

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

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

    【讨论】:

      【解决方案5】:

      在您的示例中,您将子数组相乘,但返回新数组。您需要将新数组乘以子数组。

      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 行分配的,但此时数组的每个索引都没有被初始化,所以我们真的不知道里面有什么值。

      【讨论】:

      • 我将在哪里初始化返回值?我一开始会改变第二种方法吗?
      • 我会为你澄清答案
      • 好的,我现在明白了,谢谢,但是如何将 usernumChild 的内容转移到其他方法?我认为在 multiply(usernumChild, 3000) 中声明它会这样做吗?我是否有可能摆脱第二种方法并主要执行算法,这样我就不必传递数组了?简单是目前的关键。
      • 您正确地应该在一个循环中完成所有操作。由于您可以一次完成所有工作,因此您无需为每个操作多次迭代循环。如果您不追求效率,而是易于阅读,您可以将每个操作分解为自己的方法,但我不建议这样做。
      【解决方案6】:

      使用 Java 8 流可以很简单:

      public static int[] multiply(int[] children, int number) {
      
          return Arrays.stream(children).map(i -> i*number).toArray();
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-20
        相关资源
        最近更新 更多