A. java编写一个程序实现矩阵的运算加减乘除,(并对其中的异常进行处理)
/**
*矩阵:由m×n个数Aij排成的m行n列的数表称为m行n列的矩阵,简称m×n矩阵
*说白了就是一个二维数组,下面的程序用整形作为数据类型,其他类型运算大同小异
*
*/
publicclassMatrixUtils{
/**
*矩阵运算:加(减法与之类似)
*/
publicstaticint[][]matrixAdd(int[][]addend,int[][]summand){
if(addend==null||addend.length==0){
("addendmatrixisempty!");
}
if(summand==null||summand.length==0){
("summandmatrixisempty!");
}
//矩阵加减要求两个矩阵类型一致,即行列数相同
introw=addend.length;
intcol=addend[0].length;
if(row!=summand.length||col!=summand[0].length){
("!");
}
int[][]sum=newint[row][col];
for(inti=0;i<row;i++){
for(intj=0;j<col;j++){
sum[i][j]=addend[i][j]+summand[i][j];
//sum[i][j]=addend[i][j]-summand[i][j];//减法
}
}
returnsum;
}
/**
*矩阵运算:乘法,没找到除法的运算规则
*/
publicstaticint[][]matrixMultiply(int[][]addend,int[][]summand){
if(addend==null||addend.length==0){
("addendmatrixisempty!");
}
if(summand==null||summand.length==0){
("summandmatrixisempty!");
}
//两个矩阵的乘法仅当第一个矩阵A的列数和另一个矩阵B的行数相等时才能定义。如A是m×n矩阵和B是n×p矩阵,它们的乘积C是一个m×p矩阵
introw=addend.length;
intcol=summand[0].length;
if(addend[0].length!=summand.length){
("!");
}
int[][]sum=newint[row][col];
for(inti=0;i<row;i++){
for(intj=0;j<col;j++){
for(intz=0;z<addend[0].length;z++){
sum[i][j]+=addend[i][z]*summand[z][j];
System.out.println("sum["+i+"]["+j+"]="+sum[i][j]);
}
}
}
returnsum;
}
}
B. 用java声明Matrix类表示矩阵,使用二维数组存储矩阵元素,实现以下方法:
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()// 随机取数
{
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()// 转置矩阵
{
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; i<this.mx.length; i++)
for(j=0; j<i; 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();
}
}
C. java怎么输出矩阵如输入数字3,输出3×3的矩阵
int number = 3;
for (int i = 0; i < number; i++) {
for (int k = 0; k < number; k++) {
Systrm.out.print("*");
}
System.out.println();
//或者System.out.print("\n");
}
D. java,输入一个数n,输出n*n的矩阵(正方形).例如:输入5,输出5*5的正方形
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
publicclassJuZhen{
publicstaticStringformat(intsource,intlen){
StringsourceString=source+"";
if(sourceString.length()>=len){
returnsourceString;
}else{
Stringspace="";
for(inti=0;i<(len=sourceString.length());i++){
space+="";
}
returnspace+sourceString;
}
}
publicstaticvoidmain(String[]args)throwsException{
System.out.println("请输入要输出的矩阵长度");
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
StringnumString=br.readLine();
br.close();
intnum=0;
try{
num=Integer.parseInt(numString);
}catch(Exceptione){
System.out.println("输入内容非数字");
}
if(num<=0){
System.out.println("输入数字需为正整数");
}
intlen=Integer.toString(num*num).length();
for(inti=1;i<=num;i++){
for(intj=0;j<num;j++){
intsource=i+(j*num);
Stringprint=format(source,len);
if(j==0){
System.out.print(print);
}else{
System.out.print(""+print);
}
}
System.out.println();
}
}
}
输出如下:
请输入要输出的矩阵长度
5
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
E. 用java怎么写矩阵乘法
importjava.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;i<a;i++){
for(int j=0;j<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("输入的维数不匹配,不能进行运算");
}else{
int c = x.length ;
int d = y[0].length ;
result = new double[c][d] ;
for(int i=0;i<c;i++){
for(int j=0;j<d;j++){
double sum = 0 ;
for(int k=0;k<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;i<x.length;i++){
for(int j=0;j<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("创建第一个数组:") ;
double[][] x = m.create() ;
m.print(x) ; //用来验证输入的是否和你一样的,没啥作用
System.out.println("创建第二个数组:");
double[][] y = m.create() ;
m.print(y) ; //用来验证输入的是否和你一样的,没啥作用
double[][] result = m.multiply(x, y) ;
if(result == null){
return ; //如果输入的矩阵不能运算就不输出结果了。
}
m.print(result) ;
}
}