「java实现矩阵输入」用java编写一个矩阵

博主:adminadmin 2023-03-22 13:42:08 386

今天给各位分享java实现矩阵输入的知识,其中也会对用java编写一个矩阵进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java如何输出1行3行矩阵

1、首先键盘输入矩阵的行数和列数。

2、然后再输入矩阵的内容,输出矩阵。

3、最后这样子写可以不受固定行数列数的限制,自定义行数和列数,输入内容。

java怎么输入矩阵

一般情况下都是二维数组咯, 你也可以自己用List等来存储,再用行列来控制输出

java如何输入一个自定义矩阵

java中自定义矩阵:

public static void main(String[] args) {

// TODO Auto-generated method stub

int n= 5;//长度

int array_int[][] = new int[n][n];

//初始化

for(int i=0;in;i++){

for(int j=0;jn;j++){

if(i==0){

if(j%2==0){

array_int[i][j] = (j+1)*(j+1);

}

else{

array_int[i][j] = array_int[i][j-1]+1;

}

}

else if(j==0){

if(i%2==1){

array_int[i][j] = (i+1)*(i+1);

}

else {

array_int[i][j] = array_int[i-1][j]+1;

}

}

else{

if(ij){

if(j%2==0){

array_int[i][j] = array_int[0][j]-i ;

}

else{

array_int[i][j] = array_int[0][j]+i ;

}

}

else{

if(i%2==0){

array_int[i][j] = array_int[i][0]+j ;

}

else{

array_int[i][j] = array_int[i][0]-j ;

}

}

}

//System.out.println(i+" "+j+":"+array_int[i][j] );

}

}

for(int i=0;in;i++){

for(int j=0;jn;j++){

System.out.print(array_int[i][j]+ " " );

}

System.out.println();

}

}

当等于5时的运行结果:

1 2 9 10 25

4 3 8 11 24

5 6 7 12 23

16 15 14 13 22

17 18 19 20 21

java矩阵这么键盘输入?

你好,实例代码如下,希望你可以受到启发:

import java.util.Scanner;

public class Matrix {

public static void main(String[] args) {

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;ia;i++){

for(int j=0;jb;j++){

System.out.print("请输入元素x["+i+"]["+j+"]的值:" );

x[i][j] = sc.nextDouble() ;

}

}

print(x) ;

}

public static void print(double[][] x){

System.out.println("您输入的矩阵为:");

for(int i=0;ix.length;i++){

for(int j=0;jx[i].length;j++){

System.out.print(x[i][j] + " ") ;

}

System.out.println();

}

}

}

java语言中如何在控制台上输入一个矩阵呢?

public class ABC {

public static void main(String[] args)throws java.io.IOException {

//录入部分,每个数之间用空格隔开,录完一行回车

int rows = 4;//行数

byte[] b = new byte[1024];

int read;

int[][] tmp = new int[rows][];

System.out.println("输入: ");

for(int i=0; irows; i++){

read=System.in.read(b);

String row=new String(b,0,read).trim();

String[] sp = row.split(" +");

tmp[i]=new int[sp.length];

for(int k=0;ktmp[i].length; k++)

try{

tmp[i][k]=Integer.parseInt(sp[k]);

}catch(Exception e){e.printStackTrace();}

}

//输出部分

System.out.println("输出: ");

for(int i=0; irows; i++){

for(int j=0; jtmp[i].length; j++)

System.out.print(tmp[i][j]+",");

System.out.print('\n');

}

}

}

//===============结果===============//

输入:

1 2 3 4 5

2 3 4 5 6

3 4 5 6 7

4 5 6 7 8

输出:

1,2,3,4,5,

2,3,4,5,6,

3,4,5,6,7,

4,5,6,7,8,

java实现矩阵输入的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于用java编写一个矩阵、java实现矩阵输入的信息别忘了在本站进行查找喔。