How to put 2 loop inside a list? or a second loop for a second list [closed]

-2

This is a program that verifies between 1 to 100 which numbers are divisible by 3 and also counts their quantity, all over this formula:

a=n*n;  
b=a+?;

This question is where I wanted to loop for any number from 1 to 100.

Code:

public static void main(String[] args) {

List<Long> Lista1 = new ArrayList();

for (long n = 1; n <= 100; n++) {

long a,b;

a = n*n; 
b = a+2;

if((b  % 3) == 0) {
    Lista1.add(b);
   } 

}
    System.out.println("Quantidade Números: "+Lista1.size());
    System.out.println("Divisível por 3: "+Lista1);

  }
}

It works normally:

Icannotloopin"b".
b = a+2; is equal to 67 numbers ...
If you change:
b = a+3; is equal to 33 numbers .. (quantity)
Changed, ie, would need a Print, with a list of 1 to 100 of all quantities as the formula is changed (if you can change the sum of this formula with a loop). % with% being% with%. No number from 1 to 100 is divisible by 3.

Now if you have made a list, from 1 to 3 of this "formula with loop".

result:

  

1 = 0

     

2 = 67

     

3 = 33

that is ...

b = a+1; = 0  
b = a+2; = 67  
b = a+3; = 33  

for a better understanding:

a = n * n; = number x number = 1 * 1 = 1
b = a + 2; = 1 + 2 = 3

a = n * n; = number x number = 2 * 2 = 4
b = a + 2; = 4 + 2 = 5

a = n * n; = number x number = 57 * 57 = 3249
b = a + 2; = 3249 + 2 = 3251

Answer was resolved Thanks!

    
asked by anonymous 11.08.2016 / 01:13

2 answers

0

I do not quite understand what you want to do. But see if that fixes:

public static void main(String[] args) {

    List<Long> lista = null;

    for (long j = 1; j <= 5; j++) {
        lista  = new ArrayList<>();
        for (long n = 1; n <= 100; n++) {
            long a = n*n;
            long b = a+j;
            if((b % 3) == 0) {
                lista.add(b);
            }
        }
        System.out.println("J: "+j);
        System.out.println("Quantidade Números: "+lista.size());
        System.out.println("Divisível por 3: "+lista);
    }
 }
    
11.08.2016 / 04:05
0

If your goal is to print the divisible numbers by 3 from 1 to 100, this could be done much simpler:

public static void main(String[] args) {

List<Long> Lista1 = new ArrayList();

    for (long n = 3; n <= 100; n+=3) {
        Lista1.add(n);
    }

    System.out.println("Quantidade Números: "+Lista1.size());
    System.out.println("Divisível por 3: "+Lista1);

}
    
11.08.2016 / 03:32