【问题标题】:How can I multiplicate two arrays in a way that the first value of arrayA multiply the last value of arrayB?如何以arrayA的第一个值乘以arrayB的最后一个值的方式将两个数组相乘?
【发布时间】:2019-11-24 13:49:52
【问题描述】:

如何以arrayA的第一个值乘以arrayB的最后一个值的方式将两个数组相乘?

public static void main(String[] args) throws IOException {
    int numbersA[] = new int[5];
    int numbersB[] = new int[5];
    int numbersC[] = new int[5];

    for (int i = 0; i < numbersA.length; i++) {
        System.out.print("Please insert a number for the first array: ");
        numbersA[i] = Integer.parseInt(in.readLine());
    }
    System.out.println(Arrays.toString(numbersA));

    for (int i = 0; i < numbersB.length; i++) {
        System.out.print("Please insert a number for the second array: ");
        numbersB[i] = Integer.parseInt(in.readLine());
    }
    System.out.println(Arrays.toString(numbersB));

    System.out.print("The multiplication of the two arrays (the first one with the last one and consecutively) are: ");
    for (int i = 0; i < numbersC.length; i++) {
        numbersC[i] = numbersA[i] * numbersB[(numbersB.length) - 1 - i];
    }
    System.out.println(Arrays.toString(numbersC));
}

}

【问题讨论】:

    标签: java arrays multiplication


    【解决方案1】:

    你需要从第二个数组中获取逆索引:

    for (int i = 0; i < numbersC.length; i++) {
        numbersC[i] = numbersA[i] * numbersB[numbersC.length - 1 - i];
    }
    System.out.println(Arrays.toString(numbersC));
    

    当然,这个循环依赖于所有 3 个长度相同的数组。

    【讨论】:

    • 我像你说的那样更正了我的代码,但它不起作用,我编辑了代码,以便你可以在我的问题上看到它,以便找到我的错误
    • 我更正了,但似乎还是不行,请您检查一下吗?
    【解决方案2】:

    numbersA[ ] 的第一个元素(即索引 0)应乘以 numbersB[ ] 数组的最后一个元素(即索引 9)。类似地,numbersA[ ] 的第二个元素(即索引 1)应该乘以 numbersB[ ] 的倒数第二个元素(即索引 8)。如下图:

    public static void main(String... ars) {
    
        System.out.println("enter the length of arrays");
        Scanner sc = new Scanner(System.in);
    
        int len = sc.nextInt();
    
    
        int numbersA[] = new int[len];
        int numbersB[] = new int[len];
        int numbersC[] = new int[len];
    
        Random rand = new Random();
    
        System.out.println("enter the values of first array");
        for (int i = 0; i < numbersA.length; i++) {
            numbersA[i] = sc.nextInt();
        }
        System.out.println(Arrays.toString(numbersA));
    
        System.out.println("enter the values of second array");
        for (int i = 0; i < numbersB.length; i++) {
            numbersB[i] = sc.nextInt();
        }
    
        System.out.println(Arrays.toString(numbersB));
    
    
        out.println("The multiplication of the two arrays (the first one with the last one and consecutively) are: ");
        for (int i = 0; i < numbersC.length; i++) {
            numbersC[i] = numbersA[i] * numbersB[(numbersB.length)-1-i];
        }
    
        System.out.println(Arrays.toString(numbersC));
    }
    

    【讨论】:

    • 您的代码运行良好,我的问题是数组不固定,所以我需要让用户输入它。你能检查一下我的代码吗?
    • @John 你的意思是数组的大小不是固定的吗?并且您必须将数组的长度作为用户的输入,但是两个数组的长度将相同,对吗?
    【解决方案3】:

    请尝试使用与您所做的相同的代码,并且它工作正常,正如我检查过的那样。

    公开课测试{

    public static void main(String[] args) {
    int numbersA[] = {1,2,3,4,5};
    int numbersB[] = {5,4,3,2,1};
    int numbersC[] = new int[5];
    
    
    System.out.print("The multiplication of the two arrays (the first one with the last one and consecutively) are: ");
    for (int i = 0; i < numbersC.length; i++) {
        numbersC[i] = numbersA[i] * numbersB[(numbersB.length) - 1 - i];
            System.out.println(numbersC[i]);
    }
    

    } }

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      相关资源
      最近更新 更多