values from a list in Java

-1

Have a good night!

I'm writing a code that requires a number to be multiplied by itself 200 times so far so good, but what I can not do is print 10 values on the same line for a total of 20 lines, eg

num = 7

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

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

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

Below is the code that I have achieved 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;             

    }  
 }
    
asked by anonymous 06.05.2018 / 01:34

2 answers

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));
};
    
06.05.2018 / 17:05
0

You only have to break the line when your cont variable is divisible by 20 . For this you can use the Remainder or Modulus operator represented by the % character that will return the rest of a division. That is, when you do check 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");
      }
    }
  }
}
    
21.08.2018 / 05:06