Tabuada calculation in Java, using repetition loops

1

I can not resolve the following exercise:

  

Display the results of a number table. You enter the value you want. The table should be run from 0 to 10, using the looping technique with logical testing at the end of the looping.

I can only use: DO and WHILE ..

So far my ECLIPSE code looks like this:

import java.io.IOException;
import java.util.Scanner;

public class Tabuada {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

         int A;

        Scanner tab = new Scanner(System.in);
        System.out.println("Informar um número");
        A = tab.nextInt();

    }

}
    
asked by anonymous 27.04.2018 / 16:47

2 answers

4

You only need one variable to represent the input value multiplier and to control the value of this variable within the loop .

import java.io.IOException;
import java.util.Scanner;

class Main 
{
    public static void main(String[] args) throws IOException 
    {
        int entrada;

        Scanner scanner = new Scanner(System.in);
        System.out.println("Informar um número: ");
        entrada = scanner.nextInt();

        int multiplicador = 0;
        while(multiplicador <= 10)
        {
            int resultado = entrada * multiplicador;
            System.out.println(resultado);   
            multiplicador++;
        }
    }
}

See working at repl.it     

27.04.2018 / 17:07
2

Another example, with an external loop to be able to repeat the operation of the table with other input values.

public static void main(String args[]) {

    int A;
    do {
        Scanner tab = new Scanner(System.in);
        System.out.println("Informar um número (0(zero) para finalizar):");

        A = tab.nextInt();
        if (A != 0) {
            int mult = 0;
            while (mult < 10) {
                mult++;
                System.out.println(A + " * " + mult + "  = " + (A * mult));
            }
        }
    } while (A != 0);

} 
    
27.04.2018 / 17:17