Ⅰ 用java怎麼把以下矩陣的格式列印出來
按照你的要求列印矩陣的Java程序如下
publicclassA{
publicstaticvoidmain(String[]args){
intN=3;
intn,i,j;
for(i=1;i<=N;i++){
n=1;
for(j=1;j<i;j++){
System.out.print(n+++"");
}
for(j=i;j<=2*N-1-i;j++){
System.out.print(n+"");
}
for(j=2*N-1-i+1;j<=2*N-1;j++){
System.out.print(n--+"");
}
System.out.println();
}
for(i=N-1;i>=1;i--){
n=1;
for(j=i-1;j>=1;j--){
System.out.print(n+++"");
}
for(j=2*N-1-i;j>=i;j--){
System.out.print(n+"");
}
for(j=2*N-1;j>=2*N-1-i+1;j--){
System.out.print(n--+"");
}
System.out.println();
}
}
}
運行結果
11111
12221
12321
12221
11111
Ⅱ Java輸出轉置矩陣
我按照你的情況寫了個,能運行。希望對你有幫助!
Scanner scan = new Scanner(System.in);
int [][] arry;
int m,n;
m = scan.nextInt();
n = scan.nextInt();
arry = new int [m][n];
for(int i=0;i<arry.length;i++){
for(int j = 0;j<arry[i].length;j++ ){
System.out.print("* ");
}
System.out.println();
}
scan.close();
Ⅲ java輸出5行5列矩陣
不羅嗦直接給你個代碼,注釋看不懂就問,改變東西的話,就換arr[]裡面的東西就行
public class juzheng {
public static void main(String args[]){
int arr[] = {4,2,3,7,8};//定義數組內容
int count;//計數器
int length = arr.length;//數組的長度
for(int i = 0 ;i < length;i++)
{
count =0;
for(int j = i ; count < length ; count++,j++){
if(j>length-1)
j=0;
System.out.print(arr[j]+"\t");
}
System.out.println();
}
}
}
Ⅳ JAVA怎麼輸出矩陣
根據輸入值n初始化二維數組大小 new int[2n-1][2n-1]
設置所有值為1
改變行=n的值
改變列=n的值
輸出數組
Ⅳ 用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) ;
}
}
Ⅵ 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