Hello, I have to do an exercise in which the ten multiplication tables have to appear on the screen, in this way.
I've managed to do it but I can't get the titles of the tables ("Table of 1, table of 2...) to come out right. What am I doing wrong?
Only while and if/else can be used in the exercise. Nothing for or other different things.
The code I have written is the following:
public class Main {
public static void main (String [ ] args) {
int tabla = 1;
int multiplicador = 1;
int resultado = 0;
while (tabla <= 10) {
resultado = tabla * multiplicador;
System.out.println(+tabla + "*" + multiplicador + "=" + +resultado);
multiplicador = multiplicador + 1;
if (multiplicador == 11) {
System.out.println("Tabla de multiplicar del " + tabla);
multiplicador = 1;
tabla = tabla + 1;
}
}
}
}
The truth is that you are not far from having what you need, to begin with you should show the title for the table of 1 before entering the loop
while
and adding the condition that the title of the next table is only shown up to the one of 10 :Although the previous answer is correct I believe, we can refine it to achieve the exact output result that is proposed:
Note that I use the multiplier variable to know when to show the title of the table. When the multiplier is 1, it is when the table is going to start to be written and that is why we take the opportunity to show the title first. And when the multiplier exceeds 10 it is another table and we print some separating hyphens.