「java构建矩阵」构建 矩阵
本篇文章给大家谈谈java构建矩阵,以及构建 矩阵对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
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编写矩阵
public static void main(String[] args) throws Exception {
print(create(getNum()));
}
private static int getNum() {
Scanner scanner = new Scanner(System.in);
int n = 0;
do {
System.out.println("请输入要生成的阶数:");
String input = scanner.next();
if (isDigital(input)) {
n = Integer.parseInt(input);
if (n = 0) {
System.out.println("阶数必须大于0");
}
}
} while (n == 0);
return n;
}
private static int[][] create(int n) {
int[][] num = new int[n][n];
int ax = 1, ay = 0, x = 0, y = 0;
for (int m = 1; m = n * n; m++) {
num[x][y] = m;
int tmpx = x + ax;
int tmpy = y + ay;
if (tmpx = n || tmpx 0 || tmpy = n || tmpy 0 || num[tmpx][tmpy] != 0) {
if (ax == 0) {
ax = -ay;
ay = 0;
} else if (ay == 0) {
ay = ax;
ax = 0;
}
}
x += ax;
y += ay;
}
return num;
}
private static void print(int[][] num) {
int length = String.valueOf(num.length * num.length).length();
for (int i = 0; i num.length; i++) {
for (int j = 0; j num[i].length; j++) {
String tmp = String.valueOf(num[i][j]);
while (tmp.length() length) {
tmp = " " + tmp;
}
System.out.print(tmp + " ");
}
System.out.println();
}
}
private static boolean isDigital(String input) {
if (input == null || input.length() == 0) return false;
for (int i = 0; i input.length(); i++) {
char ch = input.charAt(i);
if (!Character.isDigit(ch)) {
System.out.println("输入的阶数必须为数字");
return false;
}
}
return true;
}
运行时输入要生成的阶数就可以了,比如生成问题上的矩阵输入4就可以了。
帮忙编写一个java程序,建立一个矩阵,并能返回其中一个元素周围的值..
public class MatrixNeighbors {
private int rows;
private int columns;
private int[][] matrix = new int[rows][columns];
public int[][] getMatrix() {
return matrix;
}
public void setMatrix(int[][] matrix) {
this.matrix = matrix;
}
public MatrixNeighbors(int rows, int columns) {
super();
this.rows = rows;
this.columns = columns;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getColumns() {
return columns;
}
public void setColumns(int columns) {
this.columns = columns;
}
public String neighbours(int row, int column) {
String result = "prints: ";
if (row = rows - 1 column = columns - 1 row = 0 column = 0) {
for (int i = row - 1; i = row + 1; i++) {
for (int j = column - 1; j = column + 1; j++) {
if (i = 0 j = 0 i = rows - 1 j = columns - 1) {
if (i == row j == column) {
} else {
result += i + "," + j + "; ";
}
}
}
}
} else {
return "Error!";
}
return result;
}
}
public class Test {
public static void main(String[] args) {
MatrixNeighbors m1 = new MatrixNeighbors(2, 3);
System.out.println(m1.neighbours(1, 1));
MatrixNeighbors m2 = new MatrixNeighbors(5, 4);
System.out.println(m2.neighbours(4, 3));
System.out.println(m2.neighbours(4, 4));
}
}
java构建矩阵的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于构建 矩阵、java构建矩阵的信息别忘了在本站进行查找喔。
发布于:2022-11-23,除非注明,否则均为
原创文章,转载请注明出处。