導航:首頁 > 編程語言 > java輸出矩陣

java輸出矩陣

發布時間:2023-01-18 22:30:09

Ⅰ 用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怎麼輸出矩陣

  1. 根據輸入值n初始化二維數組大小 new int[2n-1][2n-1]

  2. 設置所有值為1

  3. 改變行=n的值

  4. 改變列=n的值

  5. 輸出數組

Ⅳ 用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&lt;a;i++){
for(int j=0;j&lt;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&lt;c;i++){
for(int j=0;j&lt;d;j++){
double sum = 0 ;
for(int k=0;k&lt;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&lt;x.length;i++){
for(int j=0;j&lt;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

閱讀全文

與java輸出矩陣相關的資料

熱點內容
公路商店app標簽選什麼 瀏覽:335
linuxoracle命令行登錄 瀏覽:224
android深度休眠 瀏覽:169
php微信開發例子 瀏覽:843
醫得app登錄密碼是什麼 瀏覽:140
spring開發伺服器地址 瀏覽:411
伺服器上如何查看伺服器的埠 瀏覽:678
單片機伺服器編譯 瀏覽:770
單口usb列印機伺服器是什麼 瀏覽:859
戰地五開伺服器要什麼條件 瀏覽:956
在word中壓縮圖片大小 瀏覽:255
javatomcat圖片 瀏覽:419
程序員生產智能創意 瀏覽:67
匯和銀行app怎麼登錄 瀏覽:383
騰訊伺服器如何上傳源碼 瀏覽:747
單片機的原理概述 瀏覽:512
火控pdf 瀏覽:269
如何復制雲伺服器centos環境 瀏覽:988
債權pdf 瀏覽:307
紅色番字的app怎麼下載 瀏覽:876