values of a Java list

Asked

Viewed 137 times

-1

Guys good night!

I’m writing a code that requires a number to be multiplied by itself 200 times so well, but what I’m not getting is to print 10 values in the same line forming a total of 20 lines per ex:

num = 7

1=7 2=14 3=21.................................................................. 10=70

11=77...........................................................................................................................................................................20=140

199=1393......................................................... 200=1400

Below is the code I got so far...

package tabela;
import java.util.Scanner;

public class Tabela {

    public static void main(String[] args) {
    Scanner entrada = new Scanner(System.in);

    int num , resp;

    System.out.println("Digite um número inteiro: ");    
    num = entrada.nextInt();

     for (int cont = 1; cont <=200; cont++ ){
         resp = num * cont;

        if((cont <=10)||(cont <= 200)){ 
        System.out.print( cont + "=" + resp+"\t" );
        }       

    }int cont = 0;             

    }  
 }
  • 1

    Edit the question by adding what you have done so far.

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

2 answers

0

You only need to break the line when your variable cont is divisible by 20. For this you can use the operator Remainder or Modulus represented by the character % which will return the rest of a division. That is, when you do the checking cont % 20 resulting in 0, line break is required:

if (cont % 20 == 0) {
  System.out.print("\n");
}

Replacing in your code:

package tabela;
import java.util.Scanner;

public class Tabela {
  public static void main(String[] args) {
    Scanner entrada = new Scanner(System.in);

    int num, resp;

    System.out.println("Digite um número inteiro: ");
    num = entrada.nextInt();

    for (int cont = 1; cont <= 200; cont++) {
      resp = num * cont;

      System.out.print(cont + "=" + resp + "\t");

      // Quebra a linha quando o cont for divisível por 20
      if (cont % 20 == 0) {
        System.out.print("\n");
      }
    }
  }
}

0

If you have a list, you can call the items like this:

ArrayList<Integer> list = new ArrayList <>();

for (int i = 0 ; i < 10 ; i++) {

        System.out.println(list.get(i));
};

Browser other questions tagged

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