本文目錄一覽:
- 1、幫忙編寫一個java程序,建立一個矩陣,並能返回其中一個元素周圍的值..
- 2、java如何輸入一個自定義矩陣
- 3、java語言中如何在控制台上輸入一個矩陣呢?
- 4、用java怎麼寫矩陣乘法?
- 5、用JAVA寫一個矩陣類
幫忙編寫一個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中自定義矩陣:
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 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怎麼寫矩陣乘法?
import java.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;ilt;a;i++){
for(int j=0;jlt;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;ilt;c;i++){
for(int j=0;jlt;d;j++){
double sum = 0 ;
for(int k=0;klt;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;ilt;x.length;i++){
for(int j=0;jlt;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是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行複雜的編程。 Java具有簡單性、面向對象、分布式、健壯性、安全性、平台獨立與可移植性、多線程、動態性等特點。Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。
用JAVA寫一個矩陣類
昨天剛幫一個網友改編的,輸出矩陣並且在矩陣求冪後輸出矩陣的一個類,直接可以運行。
注釋都有的。希望你用的得到。import java.util.Scanner;
public class JuZhen {
//定義計算方法
public static int calc(int x, int y,int score){
if(x==0 y==0){
score = 0;
}else {
score = 1;
}
return score;
}
//輸入矩陣
public static void shuru(){
Scanner input = new Scanner(System.in);//Scanner是用來接納系統控制台輸的字符串的
System.out.print(“請輸入矩陣的階數:”);
int n = input.nextInt(); //取一個輸入的字符賦值給n
int M[][] = new int[n][n]; //定義數組維數.初始化數組,定義了一個雙向的長度為
//n的
System.out.print(“請輸入矩陣的的值(0-1):”);
for(int i=0;iM.length ;i++){ //不能以0開始
for(int j=0 ;jM[i].length ; j++){
M[i][j] = input.nextInt();
}
}
System.out.println(“你輸入的矩陣為:”);
for(int i=0;iM.length ;i++){ //顯示矩陣
System.out.print(“\n”);
for(int j=0 ;jM[i].length ; j++){
System.out .print(M[i][j] + “\t”) ;
}
}
}
//僅僅是一個求冪的遞歸。
int myPow(int x, int y) {
int pow = 0;
if (y 0) {
pow = x * myPow(x, y – 1);// 2,3//2*2,3-1
}
if (y 0) {
pow = 1 / x * myPow(x, y + 1);
}
if (y == 0) {
pow = 1;
}
return pow;
}
//程序入口
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print(“請輸入矩陣的階數:”);
int n = input.nextInt();//這個相當於確定行數。
int M[][] = new int[n][n];
//定義數組維數
System.out.print(“請輸入矩陣的的值(0-1):”);
for(int i=0;iM.length ;i++){ //外循環表示行,在外循環已知的情況下去填內循環,內循環表示列
for(int j=0 ;jM[i].length ; j++){
M[i][j] = input.nextInt();
}
}
int temp[][] =new int[n][n];
int m[][] =new int[n][n];
System.out.println(“你輸入的矩陣為:”);
for(int i=0;iM.length ;i++){ //顯示矩陣
System.out.print(“\n”); //執行完外循環也就是打印出一行後換行
for(int j=0 ;jM[i].length ; j++){
temp[i][j] = M[i][j] ; //賦給矩陣temp
System.out .print(M[i][j] + “\t”) ;//執行完內循環也就是一列時空兩格。\t為tab鍵起退格作用
}
}
System.out.print(“\n\n你想求幾次方:”);
int c =input.nextInt(); //獲得冪次
for (int k=0; kc;k++){ //最外層的循環和裡邊的兩層循環也就是二維數組裡的每個都有交集,也就是每個都要求冪
for(int i=0 ; iM.length; i++){
for(int j=0; jM[i].length ;j++){
m[i][j]= new JuZhen().myPow(temp[i][j],c);
}
}
}//for k
for(int i=0;im.length ;i++){ //顯示矩陣
System.out.print(“\n”);
for(int j=0 ;jm[i].length ; j++){
System.out .print(m[i][j] + “\t”) ;
}
}
}//main
}//class JuZhen
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/271346.html