如何在 Java 中使用 for 循环时打印非对称值
How to print non symmetric value while using for loop in Java
我想使用 for 循环实现一个矩阵。为了创建矩阵,我使用了 Jama Matrix Package。
这是我的代码
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]) {
Matrix Mytest=new Matrix(5,5);
for(int i=0; i<4; i++) {
Mytest.set(i,i,1);
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
这是我的输出:
1.000000 0.000000 0.000000 0.000000 0.000000
1.000000 1.000000 0.000000 0.000000 0.000000
0.000000 1.000000 1.000000 0.000000 0.000000
0.000000 0.000000 1.000000 1.000000 0.000000
0.000000 0.000000 0.000000 1.000000 0.000000
没有编译错误或运行时错误。难点在于我怎样才能使 (0,0) 单元格值为 2?由于此矩阵是使用 for 循环构造的,因此所有值都是对称构造的。那我怎样才能让一个单元格具有不同的价值呢?
期望输出:
2.000000 0.000000 0.000000 0.000000 0.000000
1.000000 1.000000 0.000000 0.000000 0.000000
0.000000 1.000000 1.000000 0.000000 0.000000
0.000000 0.000000 1.000000 1.000000 0.000000
0.000000 0.000000 0.000000 1.000000 0.000000
我以前从未使用过 Jama,但根据 Javadoc,我认为您可以这样做:
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
for(int i=0;i<4;i++){
Mytest.set(i,i,1);
Mytest.set(i+1,i,1);
}
Mytest.set(0, 0, 2.0)
Mytest.print(9,6);
}
}
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
for(int i=0;i<4;i++){
if (i==0) {
Mytest.set(i,i,2);
} else {
Mytest.set(i,i,1);
}
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
您可以在 for 循环中使用 if 条件来为特定单元格设置不同的值。
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
for(int i=0;i<4;i++){
if(i == 0){
Mytest.set(i,i,2);
}
Mytest.set(i,i,1);
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
// first column
Mytest.set(0,0,2);
Mytest.set(1,0,1);
// others columns
for(int i=1; i<4; i++){
Mytest.set(i,i,1);
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
或
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
for(int i=0; i<4; i++){
Mytest.set(i, i, i == 0 ? 2 : 1);
Mytest.set(i+1, i, 1);
}
Mytest.print(9,6);
}
}
我想使用 for 循环实现一个矩阵。为了创建矩阵,我使用了 Jama Matrix Package。
这是我的代码
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]) {
Matrix Mytest=new Matrix(5,5);
for(int i=0; i<4; i++) {
Mytest.set(i,i,1);
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
这是我的输出:
1.000000 0.000000 0.000000 0.000000 0.000000
1.000000 1.000000 0.000000 0.000000 0.000000
0.000000 1.000000 1.000000 0.000000 0.000000
0.000000 0.000000 1.000000 1.000000 0.000000
0.000000 0.000000 0.000000 1.000000 0.000000
没有编译错误或运行时错误。难点在于我怎样才能使 (0,0) 单元格值为 2?由于此矩阵是使用 for 循环构造的,因此所有值都是对称构造的。那我怎样才能让一个单元格具有不同的价值呢?
期望输出:
2.000000 0.000000 0.000000 0.000000 0.000000
1.000000 1.000000 0.000000 0.000000 0.000000
0.000000 1.000000 1.000000 0.000000 0.000000
0.000000 0.000000 1.000000 1.000000 0.000000
0.000000 0.000000 0.000000 1.000000 0.000000
我以前从未使用过 Jama,但根据 Javadoc,我认为您可以这样做:
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
for(int i=0;i<4;i++){
Mytest.set(i,i,1);
Mytest.set(i+1,i,1);
}
Mytest.set(0, 0, 2.0)
Mytest.print(9,6);
}
}
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
for(int i=0;i<4;i++){
if (i==0) {
Mytest.set(i,i,2);
} else {
Mytest.set(i,i,1);
}
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
您可以在 for 循环中使用 if 条件来为特定单元格设置不同的值。
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
for(int i=0;i<4;i++){
if(i == 0){
Mytest.set(i,i,2);
}
Mytest.set(i,i,1);
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
// first column
Mytest.set(0,0,2);
Mytest.set(1,0,1);
// others columns
for(int i=1; i<4; i++){
Mytest.set(i,i,1);
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
或
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
for(int i=0; i<4; i++){
Mytest.set(i, i, i == 0 ? 2 : 1);
Mytest.set(i+1, i, 1);
}
Mytest.print(9,6);
}
}