我是新來的java和有一個小問題。我需要讀取一組3x3 java矩陣並打印出來。沒什麼特別的。問題是我必須閱讀它,直到我到達一個矩陣,其中包含所有元素爲null並打印所有矩陣及其行列式。我有一個閱讀空值的問題,並檢查是否都是空值。有什麼建議麼? LE:這是我到目前爲止的內容。但我需要,直到所有元素都爲空如何在Java中讀取3x3矩陣,直到所有元素都爲空?
import java.io.*;
class Matrix3x3
{
public static void main(String args[]) throws IOException
{
BufferedReader BR=new BufferedReader(new InputStreamReader (System.in));
int Number[][]=new int[3][3];
int i,j;
String m;
System.out.println("Enter Elements for Matrix 3x3 :");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
m=BR.readLine();
if (m == null)
{
m="0";
}
Number[i][j]=Integer.parseInt(m);
}
}
System.out.println("Elements in Matrix are : ");
System.out.println("");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(Number[i][j]+"\t");
}
System.out.println();
}
int determinantMatrix = determinant(Number, 3 ,3);
System.out.print("Determinant is: "determinantMatrix+"\n");
}
public static int determinant(int[][] result, int rows, int cols) {
if (rows == 2)
return result[0][0] * result[1][1] - result[0][1] * result[1][0];
int determinant1 = 0, determinant2 = 0;
for (int i = 0; i < rows; i++) {
int temp = 1, temp2 = 1;
for (int j = 0; j < cols; j++) {
temp *= result[(i + j) % cols][j];
temp2 *= result[(i + j) % cols][rows - 1 - j];
}
determinant1 += temp;
determinant2 += temp2;
}
return determinant1 - determinant2;
}
}
請更具描述性。什麼是輸入的來源?什麼是數據結構使用?試圖實現這一目標的代碼是什麼? –
如果* all *元素爲空?如果只有* some *元素爲空,您將如何計算行列式? – khelwood
輸入源是用戶輸入。如果一些元素爲空它們將被考慮爲0我猜 –