public?class?Matrix?{
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、網(wǎng)站建設(shè)、臨江網(wǎng)絡(luò)推廣、小程序設(shè)計、臨江網(wǎng)絡(luò)營銷、臨江企業(yè)策劃、臨江品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供臨江建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
//?表示行和列
private?int?mRow,?mColumn;
//?構(gòu)造方法
public?Matrix(int?row,?int?column)?{
mRow?=?row;
mColumn?=?column;
}
//?獲取0-20隨機數(shù)
private?int?random()?{
double?random1?=?Math.random();//?這個Math類的方法可以獲取0.0-1.0之間的隨機數(shù)
double?random2?=?random1?*?20;?//?0.0?-?20.0
return?(int)?random2;
}
//?創(chuàng)建矩陣
private?void?createMatrix()?{
int?totalCount?=?mRow?*?mColumn;?//?總共有那么多
for?(int?count?=?1;?count?=?totalCount;?count++)?{
int?number?=?random();//?上面的方法
System.out.print(number??10???"0"?+?number?:?number);?//?輸出數(shù)字,如果數(shù)字小于10,前面加0補全兩位
System.out.print("?");?//?分隔符,隨便填
if?(count?%?mRow?==?0)?{
System.out.println();?//?換行
}
}
}
public?static?void?main(String[]?args)?{
Matrix?matrix?=?new?Matrix(3,?5);?//?幾行幾列傳進去
matrix.createMatrix();
}
}
為了經(jīng)驗我也是蠻拼的了
/**
*?矩陣:由?m?×?n?個數(shù)Aij排成的m行n列的數(shù)表稱為m行n列的矩陣,簡稱m?×?n矩陣
*?說白了就是一個二維數(shù)組,下面的程序用整形作為數(shù)據(jù)類型,其他類型運算大同小異
*?
*/
public?class?MatrixUtils?{
/**
*?矩陣運算:加(減法與之類似)
*/
public?static?int[][]?matrixAdd(int[][]?addend,?int[][]?summand)?{
if?(addend?==?null?||?addend.length?==?0)?{
throw?new?IllegalArgumentException("addend?matrix?is?empty!");
}
if?(summand?==?null?||?summand.length?==?0)?{
throw?new?IllegalArgumentException("summand?matrix?is?empty!");
}
//矩陣加減要求兩個矩陣類型一致,即行列數(shù)相同
int?row?=?addend.length;
int?col?=?addend[0].length;
if?(row?!=?summand.length?||?col?!=?summand[0].length)?{
throw?new?IllegalArgumentException("summand?and?summand?not?the?same?type!");
}
int[][]?sum?=?new?int[row][col];
for?(int?i?=?0;?i??row;?i++)?{
for?(int?j?=?0;?j??col;?j++)?{
sum[i][j]?=?addend[i][j]?+?summand[i][j];
//?sum[i][j]?=?addend[i][j]?-?summand[i][j];?//減法
}
}
return?sum;
}
/**
*?矩陣運算:乘法,沒找到除法的運算規(guī)則
*/
public?static?int[][]?matrixMultiply(int[][]?addend,?int[][]?summand)?{
if?(addend?==?null?||?addend.length?==?0)?{
throw?new?IllegalArgumentException("addend?matrix?is?empty!");
}
if?(summand?==?null?||?summand.length?==?0)?{
throw?new?IllegalArgumentException("summand?matrix?is?empty!");
}
//兩個矩陣的乘法僅當(dāng)?shù)谝粋€矩陣A的列數(shù)和另一個矩陣B的行數(shù)相等時才能定義。如A是m×n矩陣和B是n×p矩陣,它們的乘積C是一個m×p矩陣?
int?row?=?addend.length;
int?col?=?summand[0].length;
if?(addend[0].length?!=?summand.length)?{
throw?new?IllegalArgumentException("summand?and?summand?not?the?same?type!");
}?
int[][]?sum?=?new?int[row][col];
for?(int?i?=?0;?i??row;?i++)?{
for?(int?j?=?0;?j??col;?j++)?{
for?(int?z?=?0;?z??addend[0].length;?z++)?{
sum[i][j]?+=?addend[i][z]?*?summand[z][j];
System.out.println("sum["?+?i+??"]["+?j+"]=?"?+?sum[i][j]);
}
}
}
return?sum;
}
}
import?java.util.Scanner;?
public class Matrix {?
public double[][] create() {?
Scanner sc = new Scanner(System.in) ;?
System.out.print("請輸入矩陣的行高:");?
int a = sc.nextInt() ;?
System.out.print("請輸入矩陣的列寬:");?
int b = sc.nextInt() ;?
double[][] x = new double[a][b] ;?
for(int i=0;ilt;a;i++){?
for(int j=0;jlt;b;j++){?
System.out.print("請輸入元素x["+i+"]["+j+"]的值:" );?
x[i][j] = sc.nextDouble() ;?
}?
}?
return x ;?
}?
public double[][] multiply(double[][] x,double[][] y){?
double[][] result = null ;?
int a = x[0].length ;?
int b = y.length ;?
if(a != b){?
System.out.println("輸入的維數(shù)不匹配,不能進行運算");?
}else{?
int c = x.length ;?
int d = y[0].length ;?
result = new double[c][d] ;?
for(int i=0;ilt;c;i++){?
for(int j=0;jlt;d;j++){?
double sum = 0 ;?
for(int k=0;klt;a;k++){?
sum += x[i][k]*y[k][j] ;?
}?
result[i][j] = sum ;?
}?
}?
}?
return result ;?
}?
public void print(double[][] x){?
System.out.println("矩陣為:");?
for(int i=0;ilt;x.length;i++){?
for(int j=0;jlt;x[i].length;j++){?
System.out.print(x[i][j] + " ") ;?
}?
System.out.println();?
}?
}?
}?
測試類:?
public class TestMatrix {?
public static void main(String[] args) {?
Matrix m = new Matrix() ;?
//double[][] x = {{1,2},{3,2}} ;?
//double[][] y = {{1,2,1},{2,3,3}} ;?
System.out.println("創(chuàng)建第一個數(shù)組:") ;?
double[][] x = m.create() ;?
m.print(x) ; //用來驗證輸入的是否和你一樣的,沒啥作用?
System.out.println("創(chuàng)建第二個數(shù)組:");?
double[][] y = m.create() ;?
m.print(y) ; //用來驗證輸入的是否和你一樣的,沒啥作用?
double[][] result = m.multiply(x, y) ;?
if(result == null){?
return ; //如果輸入的矩陣不能運算就不輸出結(jié)果了。?
}?
m.print(result) ;?
}?
}
Java是一門面向?qū)ο缶幊陶Z言,不僅吸收了C++語言的各種優(yōu)點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進行復(fù)雜的編程。 Java具有簡單性、面向?qū)ο?、分布式、健壯性、安全性、平臺獨立與可移植性、多線程、動態(tài)性等特點。Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
public class Matrix {
private static String matrix_A;
private int mx[][], m, n;
public Matrix(int r, int c) {
m = r;
n = c;
mx = new int[m][n];
iniMatrix();
}
public Matrix() {
m = 3;
n = 3;
mx = new int[3][3];
iniMatrix();
}
public void iniMatrix()// 隨機取數(shù)
{
int i, j;
for (i = 0; i = m - 1; i++)
for (j = 0; j = n - 1; j++)
mx[i][j] = (int) (Math.random() * 100);
}
public void tranMatrix()// 轉(zhuǎn)置矩陣
{
int i, j, t;
int mt[][] = new int[m][n];
for (i = 0; i = m - 1; i++)
for (j = 0; j = n - 1; j++)
mt[i][j] = mx[i][j];
t = m;
m = n;
n = t;
mx = new int[m][n];
for (i = 0; i = m - 1; i++)
for (j = 0; j = n - 1; j++)
mx[i][j] = mt[j][i];
}
public void printMatrix()// 輸出矩陣所有值
{
int i, j;
for (i = 0; i = m - 1; i++) {
for (j = 0; j = n - 1; j++)
System.out.print(" " + mx[i][j]);
System.out.println();
}
}
//判斷一個矩陣是否為上三角矩陣
public boolean isUpperTriangularMatrix() {
int i, j = 0;
int c = this.mx[1][0];
for(i=1; ithis.mx.length; i++)
for(j=0; ji; j++)
if(this.mx[i][j] != c)
break;
if(i=this.mx.length)
return true;
return false;
}
public void addMatrix(Matrix b)// 矩陣相加
{
int i, j;
for (i = 0; i = m - 1; i++)
for (j = 0; j = n - 1; j++)
mx[i][j] = mx[i][j] + b.mx[i][j];
}
public static void main(String args[]) {
Matrix ma = new Matrix(4, 3);
Matrix mb = new Matrix(4, 3);
System.out.println("The matrix_A:");
ma.printMatrix();
System.out.println("The matrix_B:");
mb.printMatrix();
if(ma.isUpperTriangularMatrix())
System.out.println("上三角矩陣:\n" + ma.isUpperTriangularMatrix());
System.out.println("Matrix_A + Matrix_B:");
ma.addMatrix(mb);
ma.printMatrix();
System.out.println("Transpose Matrix_A:");
mb.tranMatrix();
mb.printMatrix();
System.out.println("Transpose Matrix_A+Matrix_B:");
mb.tranMatrix();
mb.printMatrix();
}
}
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("請輸入n*n數(shù)組,n=");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] a = new int[n][n];//定義n*n數(shù)組
int result = 0;
for(int i=0; in; i++){
for(int j=0; jn; j++){
System.out.println("請輸入第"+i+"行,第"+j+"列元素:");
a[i][j]=sc.nextInt();
}
result +=a[i][i];
}
System.out.println("對角線元素和為:"+result);
}
}