Java 我的矩阵没有’;当我尝试执行标量乘法时,它不会改变
我们应该用矩阵中的每个元素乘以一个数字,在这个例子中是“k”。我不确定我做错了什么,但它不起作用。救命啊!到目前为止,我已经编辑并添加了整个项目Java 我的矩阵没有’;当我尝试执行标量乘法时,它不会改变,java,arrays,matrix,Java,Arrays,Matrix,我们应该用矩阵中的每个元素乘以一个数字,在这个例子中是“k”。我不确定我做错了什么,但它不起作用。救命啊!到目前为止,我已经编辑并添加了整个项目 public class Matrix { private int r;//rows private int c;//columns private int[][] neo; //2D array public static void main(String[] args) { Matrix m1 =
public class Matrix {
private int r;//rows
private int c;//columns
private int[][] neo; //2D array
public static void main(String[] args) {
Matrix m1 = new Matrix(3,4);
Matrix m2 = new Matrix(3,4);
System.out.println(m2);
try {
Matrix m3 = m1.multiply(m2);
} catch (Exception e) {
e.printStackTrace();
}
m2.scaleMult(k);
}//main
public Matrix(int row, int column) {
r = row;
c = column;
neo = new int[r][c];
for(int i = 0; i < neo.length; i++) {
for (int j = 0; j < neo[i].length; j++) {
neo[i][j] = (int) (1 + Math.random() * 100);
}//forLoop
}//forLoop
System.out.println(Arrays.toString(neo));
}//Matrix
public Matrix copyMatrix(Matrix m) {
Matrix copy = new Matrix(m.r, m.c);
for (int i = 0; i < r; i++) {
System.arraycopy(this.neo[i], 0, copy.neo[i], 0, this.neo[i].length);
}//forLoop
return copy;
}//copyMatrix
public void scaleMult(int k){
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
this.neo[i][j] * k;
}//scaleMult
public boolean equals(Matrix m2) {
if (this.r != m2.r || this.c != m2.c) {
return false;
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (this.neo[i][j] != m2.neo[i][j]) {
return false;
}
}
}
return true;
}//equalsMethod
public Matrix multiply(Matrix m2) throws Exception {
if (this.c != m2.r) {
throw new RuntimeException();
}//if
Matrix m3 = new Matrix(this.r, m2.c);
for (int i = 0; i < this.r; i++) {
for (int j = 0; j < m2.c; j++) {
m3.neo[i][j] = 0;
for (int k = 0; k < this.c; k++) {
m3.neo[i][j] += this.neo[i][k] * m2.neo[k][j];
}//forK
}//forJ
}//forI
return m3;
}//multiplyMethod
}//class
公共类矩阵{
私有int r;//行
私有int c;//列
私有int[][]neo;//二维数组
公共静态void main(字符串[]args){
矩阵m1=新矩阵(3,4);
矩阵m2=新矩阵(3,4);
系统输出打印项数(m2);
试一试{
矩阵m3=m1乘以(m2);
}捕获(例外e){
e、 printStackTrace();
}
m2.标度单位(k);
}//主要
公共矩阵(整数行,整数列){
r=行;
c=列;
neo=新整数[r][c];
for(int i=0;i
如果你的问题是将一个矩阵乘以一个数字,那么这里有一个非常简单的例子
public static void main(String []args){
int[][] a = {{1,2,3},{4,5,6}};
int[][] b=new int[2][3];
int i,j,k=2;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
b[i][j]=a[i][j]*k;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
System.out.println(b[i][j]);
}
publicstaticvoidmain(字符串[]args){
int[]a={{1,2,3},{4,5,6};
int[]b=新int[2][3];
int i,j,k=2;
对于(i=0;i将scaleMult
方法更改为此,它应该可以工作:
public void scaleMult(int k) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
this.neo[i][j] *= k;
// you forgot to assign the value, basically we multiply neo[i][j] by k and make neo[i][j] receive that value
// making this.neo[i][j] = this.neo[i][j] *k; would work as well
}
}
}//scaleMult
public void scaleMult(int k){
对于(int i=0;i
此外,根据您在main
方法上提供的示例,您的代码将在multiply
方法上启动一个异常,因为正如您指定的,如果一个矩阵的列数等于另一个矩阵的行数,则只能将两个矩阵相乘。除此之外,我建议仅为您的主方法创建一个新类od.你能粘贴你尝试过的完整代码吗?你会遇到什么错误,矩阵类和乘法方法一起丢失错误是scaleMult方法中的“this.neo[i][j]*k;”。我真的不知道为什么,我需要“this.k=k;”语句吗?先生,你是我的英雄:)我会熬夜一整晚,因为明天就要交了。再次感谢你为什么你需要为主方法分离类?该方法是静态的,所以它不需要一个矩阵来工作。我不是说在同一个类中使用主方法是个好主意,但在一个相当独立的类中,我可以接受这一点这是调试类的一个好方法。我已经见过很多次了。我尝试在IDE中使用它,但它根本不起作用(项目刚刚启动并立即关闭)。这可能与我如何设置所有内容有关。