How to make a program of tables with command structures restriction?

Asked

Viewed 228 times

3

This program is for axiliar a person to see the tables, the intention is to offer 2 options:

Case 1: Table 1 to 10 is returned; Case 2: No the multiplication table of a specific number.

However the program does not perform operations.

Additional detail: only the following structures are allowed to be used: java.util.Scanner, while, if/else and switch case.

package Estruturas_de_Repetição_I;
import  java.util.Scanner;
public class Tavanna {//Programa de auxilio ao estudo da tabuada

    private static Scanner ler;

    public static void main(String[] args) {

    //Declaração das variáveis
    int valor = 0, option; 

    //Configurar leitor de dados
    ler = new Scanner(System.in);

    //Mensagem ao usuário + Data input
    System.out.printf("Digie 1 para ver todas as tabuadas. \n"
            + "Digite 2 para ver a tabuda de um número.", option);

    //Ler opção
    option = ler.nextInt();

        if (option == 1){//Caso seja 1
            while(valor <= 10);{ //condição para sair do laço
            for (int i=0; i<=10;i++) {
                System.out.println(valor + " X " + i + " = " + (valor*i));//Teoricamente era passar o valor de todas as tabuadas de 0 a 10
                valor = valor++;
                break;
            }
            }
        } else if (option == 2) {//Caso seja 2
        for (int i=0; i<=10; i++);{ //laço para repetição
        System.out.printf("Qual tabuada você prescisa saber", valor);
        valor = ler.nextInt();
        System.out.println(valor + " X " + i + " = " + (valor * i));//Teoricamente seria para passar a tabuada de valor escolhido       
        }
        }
    }
}
  • There were typos in the code, so it didn’t work.

2 answers

2


There are some errors in this code.

The first is the semicolon right after the while and for:

while(valor <= 10);{
                  ^ aqui
for (int i=0; i<=10; i++);{
                         ^ aqui

When you put that ; shortly after the while or for, it is interpreted as an empty block (i.e., within the for and of while has nothing to run).

And there the { then opens a code block, but that is not part of the loop. So the first thing is to remove this ;.

Another problem is this line:

valor = valor++;

This does not increase the valor, on the contrary, it always remains the same (see here the explanation):

int valor = 0;
valor = valor++;
System.out.println(valor);
valor = valor++;
System.out.println(valor);

The code above prints 0 twice, see. If you want to increase the value, simply do valor++ (or valor += 1, whatever).

The break within the first for doesn’t make sense, because break interrupts the loop, but you don’t want to interrupt it. Anyway, there are other logic errors that could be detected by making a table test.

It was unclear if you can use methods, so a version without using them would look like this (the question does not mention that you can use for - only while - but as you used, so I will also use):

Scanner ler = new Scanner(System.in);
System.out.println("Digite 1 para ver todas as tabuadas.\nDigite 2 para ver a tabuada de um número.");
int option = ler.nextInt();
if (option == 1) {
    int valor = 1;
    while (valor <= 10) {
        for (int i = 0; i <= 10; i++) {
            System.out.println(valor + " X " + i + " = " + (valor * i));
        }
        valor++;
    }
} else if (option == 2) {
    System.out.printf("Qual tabuada você quer saber?");
    int valor = ler.nextInt();
    for (int i = 0; i <= 10; i++) {
        System.out.println(valor + " X " + i + " = " + (valor * i));
    }
}

If you can use methods, create one to print the table of a specific number, so you can reuse it:

public class Tabuada {
    static void tabuada(int valor) {
        for (int i = 0; i <= 10; i++) {
            System.out.println(valor + " X " + i + " = " + (valor * i));
        }
    }

    public static void main(String[] args) throws Exception {
        Scanner ler = new Scanner(System.in);
        System.out.println("Digite 1 para ver todas as tabuadas.\nDigite 2 para ver a tabuada de um número.");
        int option = ler.nextInt();
        if (option == 1) {
            int valor = 1;
            while (valor <= 10) {
                tabuada(valor);
                valor++;
            }
        } else if (option == 2) {
            System.out.printf("Qual tabuada você quer saber?");
            tabuada(ler.nextInt()); // nem precisa da variável, pode passar o valor direto para o método
        }
    }
}
  • Amabas solutions gave right here, I believe that the main mistake, is my poor typing of the code, xtendo a little more. There is something I can add to make the tables in option 1 appear side by side.

  • @Patrikrufino Then you have to change the logic of the loops a little, and first print all the numbers multiplied by zero, then all multiplied by 1, etc. Something like this: https://ideone.com/mNjTkt

  • Got it, thanks a lot for your help.

  • I reworked the program, take a look at it, it’s working the way I wanted it to.

1

After the tips the program was as follows, the only thing, that I could not it was tabular in sequence, but jumps of line to each new tabulation in option 1.

 package Estruturas_de_Repetição_I;
 import     java.util.Scanner;
 public class Tavanna {//Programa de auxilio estudo da tabuada

private static Scanner ler;

public static void main(String[] args) {
//Declaração das variáveis
int option = 0; 

//Configurar leitor de dados
ler = new Scanner(System.in);

//Mensagem ao usuário + Data input
System.out.printf("Bem vindo ao Tavanna!\n"
        +"\nAqui você pode ver as tabuadas, escolha sua opção:\n"
        + "\nDigie 1 para ver todas as tabuadas de 1 a 10. \n"
        + "Digite 2 para ver a tabuda de um número específico. \n"
        + "\nDigite sua opção aqui =>"
        + "", option);

//Ler opção
    option = ler.nextInt();

    if (option == 1){ //Caso seja 1
        int valor = 1;
        while(valor <= 10) {  //condição para sair do laço
            for (int i=0; i<=10;i++) {
                System.out.println(valor + " X " + i + " = " + (valor*i));
            }
            valor ++;
            System.out.println("\t");
        }
    } else if (option == 2) {//Caso seja 2
            System.out.printf("\nQual tabuada você prescisa saber?\n"
                    + "\nEssa é a tabuada do =>");
            int valor = ler.nextInt();
            for (int i = 0; i <= 10; i++) {
            System.out.println(valor + " X " + i + " = " + (valor * i));        
    }
    }
}

}

  • He jumps line because System.out.println always inserts a line break at the end. If you don’t want me to jump line, use System.out.print, as I did here <<< also note that the \t It should be inside the inner loop, not outside like you did. Anyway, the idea of the site is not to keep putting up codes for discussion or evaluation. Here you pose a specific problem and people respond (as we did above). If you have another question - even if related - then it is the case of ask another question :-)

  • Although in this case, I think the link I passed (https://ideone.com/mNjTkt) already solves your problem, so I wouldn’t have to ask you a new question...

Browser other questions tagged

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