Repeating structure

Asked

Viewed 112 times

3

I’m studying repetition structure in Java here. And I came across the will to do the following:

public class Teste {

    public static void main(String[] args) throws IOException {

        for (int i = 1; i <= 2; i++) {
            for (int j = 1; j <= 2; j++) {
                System.out.println("Linha: " + j);
            }
            System.out.println(" Registro: " + i);
        }

    }
}

The exit from this stretch is:

Linha: 1
Linha: 2
    Registro: 1
Linha: 1
Linha: 2
   Registro: 2

And the desired exit would be:

Linha: 1
Linha: 2
    Registro: 1
Linha: 3
Linha: 4
   Registro: 2

That is, that in the second turn of the first loop the j was not zeroed, thus generating Line 3 and Line 4 for the second record.

How do I do that?

  • Just change the first one to for (int i = 1; i <= 4; i++)

1 answer

1


You can do it that way:

for (int i = 1; i <= 4; i++) {

    System.out.println("Linha: " + i);

    if(i%2==0){
        System.out.println(" Registro: " + (i/2));  
    }
}

See it working on ideone: https://ideone.com/3gwhSD


Or to have control of the registration number and lines independently, just do the form below:

int linhas = 3, 
registros = 2, 
temp = 1;

for (int i = 1; i <= registros; i++) {

    for(int j = 0; j < linhas; j++){
        System.out.println("Linha: " + (temp++));
    }
        System.out.println(" Registro: " + i);
}

The result of this example will be:

Linha: 1
Linha: 2
Linha: 3
 Registro: 1
Linha: 4
Linha: 5
Linha: 6
 Registro: 2

See this example working on ideone: https://ideone.com/3lkxOc

  • Putz, interesting! I will apply some use cases here with variant values! Show :)

  • Yeah. Okay, but when I want to edit the number of lines and/or records the module operator doesn’t work.

  • @Samivasconcelos ai you complica me hehe, I made the solution based only on the problem you reported in the question. This is why the solution cannot always meet other cases, even if they are similar :/

  • hahaaha.. is part! The business is to challenge! =)

  • @Samivasconcelos out of curiosity, what change did you try to make that did not work?

  • If you vary, for example, the number of records it already goes..

  • @Samivasconcelos but what is the new pattern? Without a definite pattern, it is very complicated to think of a logical solution. In the answer, the pattern is 2 lines for each record. If you change the loop to higher even values(6,8,10...) you will see that the pattern will continue. if you put an odd value, you will have line left without subsequent record.

  • So, thinking outside the same pattern. Now, I assume that the number of rows and the number of records is assumed by a variable. A value entered at execution time.. complicated, right? Burning the mufa here..

  • I can assume that each record will have 1000 lines, for example. I closed the first record on line 1000 and started the new record of line 1001 until 2001, for example.

  • @Samivasconcelos simples: https://ideone.com/3lkxOc. Therefore, no matter the number of lines or records, both are independent of each other, different from the answer code.

  • Yes. For the first case, it was ok. With the change it no longer works. But, quiet! That’s right. Things can always get worse.. I find it interesting to think of these variations. trying to solve here.

  • @Samivasconcelos you saw the link I posted in the previous comment? https://ideone.com/3lkxOc do the test varying with this code of this link. I tested with variations up to 50 and it worked

  • Shit, man! I hadn’t seen it.. When I see this simplicity in the code, I realize that I’m loooonge of it gets good! hauahauaah Putz, mto fera!

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.