想用矩阵解一个最常见的线性方程组,还没有完成,大侠们帮忙看看错哪了???
import java.util.Scanner;class XY
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
System.out.println("请输入方程的维数:");
int n = reader.nextInt();
double a[][] = new double[n][n+1];
System.out.println("请输入方程矩阵:");
for (int i=0;i<n ;i++ )
{
for (int j=0;j<n+1 ;j++ )
{
a[i][j] = reader.nextDouble();
}
}
Matrix matr =new Matrix(n,a);
matr.ShowMatrix();
Finalmatr(matr,n);
}
//把矩阵化为三角
public static double Finalmatr(double matr[][],int n)
{
for (int i=0;i<n-1 ;i++ )
{
for (int m=i+1;m<n ;m++ )
{
Xline(matr,i,m,n,-matr[m][i]/matr[i][i]);
}
}
}
//第i行乘以K与另一行相加
public static double Xline(double matr[][],int i,int m,double k)
{
for (int j=0;j<n+1 ;j++ )
{
matr[m][j] = matr[i][j]*k +matr[m][j];
}
return matr[][];//提示这里错了
}
}
//只考虑方程数等于未知数个数,创建矩阵
class Matrix
{
static int n;//n维线形方程组
static double[][] matr = new double[n][n+1];
public Matrix(int n,double a[][])
{
this.n = n;
matr = a;
}
public static void ShowMatrix(){
for (int i=0;i<n ;i++ )
{
//控制输出格式
for (int j=0;j<n+1 ;j++ )
{
System.out.print(" "+matr[i][j]);
if (j==n)
{
System.out.println();
}
}
}
}
}
import java.util.Scanner;
class XY
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
System.out.println("请输入方程的维数:");
int n = reader.nextInt();
double a[][] = new double[n][n+1];
System.out.println("请输入方程矩阵:");
for (int i=0;i<n ;i++ )
{
for (int j=0;j<n+1 ;j++ )
{
a[i][j] = reader.nextDouble();
}
}
Matrix matr =new Matrix(n,a);
matr.ShowMatrix();
Finalmatr(matr,n);
}
//把矩阵化为三角
public static double Finalmatr(double matr[][],int n)
{
for (int i=0;i<n-1 ;i++ )
{
for (int m=i+1;m<n ;m++ )
{
Xline(matr,i,m,n,-matr[m][i]/matr[i][i]);
}
}
}
//第i行乘以K与另一行相加
public static double Xline(double matr[][],int i,int m,double k)
{
for (int j=0;j<n+1 ;j++ )
{
matr[m][j] = matr[i][j]*k +matr[m][j];
}
return matr[][];
}
}
//只考虑方程数等于未知数个数
class Matrix
{
static int n;//n维线形方程组
static double[][] matr = new double[n][n+1];
public Matrix(int n,double a[][])
{
this.n = n;
matr = a;
}
public static void ShowMatrix(){
for (int i=0;i<n ;i++ )
{
//控制输出格式
for (int j=0;j<n+1 ;j++ )
{
System.out.print(" "+matr[i][j]);
if (j==n)
{
System.out.println();
}
}
}
}
}
XY.java:48: 需要 ".class"
return matr[][]; matrix class import
补充:Java , Java SE