gpt4 book ai didi

java - 如何通过构造函数传递数组值

转载 作者:行者123 更新时间:2023-12-02 04:45:00 26 4
gpt4 key购买 nike

我的代码将数组中的两个矩阵相乘并输出值。接下来我想做的是将代码分成两个类。我想要一个可以接受用户输入并将数组值传递给另一个类中的构造函数,然后计算相同值的类。我知道如何从第二个类获取返回类型,但不知道如何传递数组中的值进行计算。任何帮助,将不胜感激。谢谢!!

import java.util.Scanner;

class MatrixApp
{
public static void main(String args[])
{
int m, n, p, q;
int sum = 0, c, d, k;

Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first matrix");
m = in.nextInt();
n = in.nextInt();

int first[][] = new int[m][n];

System.out.println("Enter the elements of first matrix");

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();

System.out.println("Enter the number of rows and columns of second matrix");
p = in.nextInt();
q = in.nextInt();

if ( n != p )
System.out.println("Matrices with entered orders can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];

System.out.println("Enter the elements of second matrix");

for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
second[c][d] = in.nextInt();

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

System.out.println("Product of entered matrices:-");

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
System.out.print(multiply[c][d]+"\t");

System.out.print("\n");
}
}
}
}

最佳答案

我不确定我是否理解正确,但是如果您想将数组值传递给构造函数并进行计算,那么下面是一个这样的实现,我根据您的需要使用了两个类。这是一个非常快的实现,所以我没有对其进行优化。

import java.util.Scanner;

公共(public)类 TestMatrixApp {

/**
* @param args
*/
public static void main(String[] args) {
int m, n, p, q;

Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first matrix");
m = input.nextInt();
n = input.nextInt();

System.out
.println("Enter the number of rows and columns of second matrix");
p = input.nextInt();
q = input.nextInt();

System.out.println("Enter the elements of first matrix");

int first[][] = new int[m][n];
for (int c = 0; c < m; c++)
for (int d = 0; d < n; d++)
first[c][d] = input.nextInt();

System.out.println("Enter the elements of second matrix");
int second[][] = new int[p][q];

for (int c = 0; c < p; c++)
for (int d = 0; d < q; d++)
second[c][d] = input.nextInt();

MatrixApp matrixApp = new MatrixApp(first, second, m, n, p, q);
}

}

下一个类

公共(public)类 MatrixApp {

public MatrixApp(int first[][], int second[][], int m, int n, int p, int q) {
doMatrixMultiply(first, second, m, n, p, q);
}

public void doMatrixMultiply(int first[][], int second[][], int m, int n, int p, int q)
{

if ( n != p )
System.out.println("Matrices with entered orders can't be multiplied with each other.");
else
{
int multiply[][] = new int[m][q];
int sum = 0;
for ( int c = 0 ; c < m ; c++ )
{
for (int d = 0 ; d < q ; d++ )
{
for (int k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

System.out.println("Product of entered matrices:-");

for (int c = 0 ; c < m ; c++ )
{
for (int d = 0 ; d < q ; d++ )
System.out.print(multiply[c][d]+"\t");

System.out.print("\n");
}
}
}
}

关于java - 如何通过构造函数传递数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29734687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com