Java Tabulated Calculation, using Repeat Loops

Asked

Viewed 5,118 times

0

I’m having trouble solving the following exercise:

Display the tabulated results of any number. The user informs what value he wants. The tabulation should be performed from 0 to 10, using the loop loop technique with logic test at the end of the looping.

I can only use : DO and WHILE..

For now my code in ECLIPSE is 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();

    }

}
  • 1

    Can you write the code with some kind of loop, for example with for? Because in your code there is nothing related to the solution attempt yet.

3 answers

5


You only need one variable to represent the multiplier of the input value and 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 on repl.it

  • It can only use do-while. Also, simply implementing to the person is not exactly the purpose of the site.

  • @prmottajr Logic remains exactly the same. I’m not here to do third party work. Btw, I explained exactly how the code works, it’s simple, there’s nothing to be fooled about. I know very well the purpose of the site.

  • They are separate things. I commented on the question asking to be informed what had already been tried. The way the question was asked was "do it for me". I only commented about not being the purpose because I had already indicated in the question itself that the collaboration was not exactly like this. And it wasn’t my goal to offend.

  • @prmottajr Yes, I know. What I meant to say to you is that there is not much to try to explain without making the code. Anyway, I’ll try to edit and explain better.

2

Another example, with an external loop to be able to repeat the tab operation 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);

} 
  • The statement says nothing about looping through other entries. For the main logic, you used while and not do-while. And the purpose of the site is to help in implementations, not to give them ready when the person has not shown something concrete.

  • @prmottajr example shows the two ways to use while. I don’t expect the author to simply take my code, but if he does, I see no problem. With his words "the site proposal is to help in implementations"

  • I commented on the question asking to be informed what had already been tried. The way the question was asked was "do it for me". So I commented here, the problem is not in the answers themselves, but in the way the question was posed. In the end, the answers end up encouraging the search for exercise solutions without the person presenting what tried to do.

0

Can also be done in the form below:

public Static void main(String[] args) {

    int valorUsuario;
    int multiplicador = 0;
    int total = 0;


    Scanner ler = new Scanner(System.in);
    System.out.println("Informe um número: ");
    valorUsuario = ler.nextInt();

    for (int x = 0; x < 10; x++) {
            total = valorUsuario * ++multiplicador;
            System.out.println(valorUsuario + " x " + multiplicador + " = " + total);
        }

    }

Browser other questions tagged

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