Does Apple copy error in SWITCH statement?

It drives me crazy! I was looking for my code and I cannot solve this problem. Here is my code:

class Subtarefas {
public static void resolve (int flag, int na, Aeroporto a[], int nv, Voo v[]) {
    switch(flag) {
        //------------------------------------------------------------------
        case 3: {//Incompleto
            String mat[][] = new String [na][2];
            int count=0;
            int bigcount=0;
            int indice=0;
            int np=0;

            for (int i=0; i<na; i++) {
                if ( indexOf(np, mat, a[i].nomecidade, a[i].nomepais)== -1 ) {
                    mat[np][0]=a[i].nomecidade;
                    mat[np][1]=a[i].nomepais;
                    np++;
                }
            }

            for (int i=0; i<np; i++) {
            count=0;
                for (int j=0; j<np; j++) {
                    if (mat[i][1].equals(mat[j][1])) count++;
                }
                if (count>bigcount) {bigcount=count; indice=i;}
            }

            System.out.println(mat[indice][1] + " " + bigcount);
        }
        //------------------------------------------------------------------
        case 4: {//Feito
            String mat1[][] = new String [nv][2];
            int count=0;
            int bigcount=0;
            int indice=0;

            for (int i=0; i<nv; i++) {
                for (int j=0; j<na; j++) {
                    if (v[i].origem==a[j].cod) mat1[i][0]=a[j].nomepais;
                    if (v[i].destino==a[j].cod) mat1[i][1]=a[j].nomepais;
                }
            }

            for (int i=0; i<na; i++) {
            count=0;
                for (int j=0; j<nv; j++) {
                    if (a[i].nomepais.equals(mat1[j][0])) {
                        if (mat1[j][0].equals(mat1[j][1])) count++;
                    }
                }
                if (count>bigcount) {bigcount=count; indice=i;}
                else if (count==bigcount) {
                    int result = a[i].nomepais.compareTo(a[indice].nomepais);
                    if (result<0) {bigcount=count; indice=i;}
                }
            }

            System.out.println(a[indice].nomepais + " " + bigcount);
        }
        //------------------------------------------------------------------
        default: break;
    }
}

So basically, I can’t find what is wrong here, the code is too big, and my head cannot filter brackets like {}. There is some kind of error here, because when I use case 3, I get two outputs, and I have to give me only 1 (in this case I only have 1 System.out.println).

I found out when I use case 3, it also goes through case 4. This should not happen! What should I do?

Another option is that there may be an opening bracket (without closing). But I can’t find him!

Can you help me?

Thank.

+4
source share
2

break;, , switch case

+12

:

switch(flag) {
    case 3: //Feito
    // your code;
    break;  // This makes it stop the switch
    case 4: //Feito 
    // your code;
    break;  // put break here if more case follows  
}
+1

Source: https://habr.com/ru/post/1533813/


All Articles