[英]Java do-while detail
My code does an error but I don't know how to correct it: 我的代码出错,但我不知道如何纠正它:
public class Cenas {
public static void main(String[] args) {
//gera matrizes de tamanho aleatório com uns e zeros
int a = 2;//(int)(Math.random() *3) + 1;
int b = 2;//(int)(Math.random() *3) + 1;
int[][]matriz = new int [a][b];
do{
for (int i=0; i<a; i++) {
for (int j=0; j<b; j++) {
matriz[i][j] = (int) Math.round(Math.random());
System.out.print(matriz[i][j] + " ");
}
System.out.println("");
}
}while(matrizIdentidade(matriz)==true); //the error is in here!!! the ";"
public static boolean matrizIdentidade (int[][]m){
boolean diagonal = false;
if (m.length==m[0].length) //matriz.Testada[0] é o comprimento da matriz
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
if(i==j && m[i][j]==1)
if(i!=j && m[i][j]==0)
diagonal = true;
return diagonal;
}
}
It generates random matrices and tells me if they are an identity matrix or not. 它生成随机矩阵并告诉我它们是否是一个单位矩阵。 I put the System.out.print and dimension 2 by 2 just for testing.
我把System.out.print和尺寸2加2只是为了测试。 The error makes my loop infinite...
错误使我的循环无限...
";" “;” on the line commented appears underlined in red (in Eclipse) giving me an error.
在评论的行上显示红色下划线(在Eclipse中)给我一个错误。
I humbly think that you are missing my question. 我谦卑地认为你错过了我的问题。 I don't know if the statements in my methods are correct (I'm working on it), but what brings me here is that the ";"
我不知道我的方法中的陈述是否正确(我正在研究它),但是我带来的是“;” gives me an error: "Syntax error, insert "}" to complete MethodBody".
给我一个错误:“语法错误,插入”}“以完成MethodBody”。 If that is due to my badly coded logic I apologized.
如果这是由于我编码错误的逻辑,我道歉。 But I think, instead, that indicates that I'm doing some syntax error on the do-while loop.
但我认为,相反,这表明我在do-while循环上做了一些语法错误。
You are defining the return value only by the last member 您只能由最后一个成员定义返回值
diagonal = true;
Should be the opposite, you start assuming the matrix is the identity and return false when you check that it is not true. 应该相反,你开始假设矩阵是身份,当你检查它不是真时返回false。
if((i==j && m[i][j]!=1) || (i!=j && m[i][j]!=0)) {
// this is not an identity matrix, so you can stop
return false;
}
if the loop ends, the matrix is the identity so return true
. 如果循环结束,矩阵是标识,所以返回
true
。
The loop do-while is infinite because it never get matrizIdentidade(matriz)==true
. 循环do-while是无限的,因为它永远不会得到
matrizIdentidade(matriz)==true
。
Try to browse the elements of the matrix twice to check the two properties of a diagonal matrix. 尝试浏览矩阵的元素两次以检查对角矩阵的两个属性。 Try this please :
请试试这个:
public static boolean matrizIdentidade (int[][]m){
boolean diagonal1 = false, diagonal2 = false ;
if (m.length==m[0].length) //matriz.Testada[0] é o comprimento da matriz
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
if(i!=j && m[i][j]==0)
diagonal1 = true;
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
if(i==j && m[i][j]==1)
diagonal2 = true;
if(diagonal1 == true && diagonal2 == true ) return true
else return false;
}
If the problem is related to the syntax of do-while
check this documentation Snippet : 如果问题与
do-while
的语法有关do-while
请查看本文档的 Snippet:
do {
System.out.println("Count is: "
+ count);
count++;
} while (count < 11);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.