代码如下:
创新互联是一家网站设计公司,集创意、互联网应用、软件技术为一体的创意网站建设服务商,主营产品:响应式网站建设、品牌网站制作、营销型网站建设。我们专注企业品牌在网站中的整体树立,网络互动的体验,以及在手机等移动端的优质呈现。网站建设、成都网站建设、移动互联产品、网络运营、VI设计、云产品.运维为核心业务。为用户提供一站式解决方案,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏析悦目的作品,网站的价值服务。
import java.util.Random;
public class App26 {
static int[][] creat() {
int[][] arr = new int[3][4];
Random random = new Random(System.currentTimeMillis());
for (int i = 0; i 3; i++) {
for (int j = 0; j 4; j++) {
arr[i][j] = random.nextInt(100);
}
}
return arr;
}
static void output(int[][] arr) {
for (int i = 0; i 3; i++) {
for (int j = 0; j 4; j++) {
System.out.print(String.format("%2d ", arr[i][j]));
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] arr = creat();
int max = Integer.MIN_VALUE;
for (int i = 0; i 3; i++) {
for (int j = 0; j 4; j++) {
if (arr[i][j] max) {
max = arr[i][j];
}
}
}
System.out.println("矩阵中最大值是:" + max);
output(arr);
}
}
没看到你的图,试试下面这个是不是你想要的:
#include
int main()
{
int i ,j;
int a[10][10];
for(i=0;i10;i++)
for(j=0;j10;j++)
{
a[i][j]=0;
if(i==(9-j)j==(9-i))a[i][j]=1;
if(i==j)a[i][j]=1;
}
for(i=0;i10;i++)
{
for(j=0;j10;j++)
{
printf("%i ",a[i][j]);
}
printf("\n");
}
getch();
}
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中自定义矩阵:
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
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就可以了。