1

私のコードはエラーを出しますが、それを修正する方法がわかりません:

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;
}
}

ランダム行列を生成し、それらが単位行列であるかどうかを教えてくれます。テストのために、System.out.printとdimension 2x2を配置しました。エラーにより、ループが無限ループになります...

";" コメントされた行に、(Eclipseでは)赤の下線が引かれ、エラーが発生します。

私はあなたが私の質問を見逃していると謙虚に思います。私のメソッドのステートメントが正しいかどうかはわかりませんが(私はそれに取り組んでいます)、ここで私をもたらすのは「;」です。エラーが発生します:「構文エラー、「}」を挿入してMethodBodyを完了します」。それが私のひどくコーディングされたロジックによるものであるならば、私は謝罪しました。しかし、代わりに、do-whileループで構文エラーが発生していることを示していると思います。

4

2 に答える 2

4

最後のメンバーによってのみ戻り値を定義しています

  diagonal = true;

逆の場合は、行列がIDであると想定し始め、それが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;
}

ループが終了した場合、行列はIDであるため、を返しtrueます。

于 2013-01-05T14:53:23.757 に答える
0

ループdo-whileは、決して取得されないため、無限ですmatrizIdentidade(matriz)==true

対角行列の2つのプロパティを確認するには、行列の要素を2回参照してみてください。これを試してください:

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; 
}

問題が構文に関連している場合は、このドキュメントdo-whileを確認してください 。

       do {
            System.out.println("Count is: "
                               + count);
            count++;
        } while (count < 11);
于 2013-01-05T14:53:47.457 に答える