Loop is not repeating as it should

Asked

Viewed 280 times

3

I am creating a code that reads the number typed by the user and, if it is from 1 to 10, it performs the tabulation, presenting value to value on the screen, besides allowing the user to choose whether to check some other tabular.

The first time, it goes smoothly. However, if the user chooses to make a new query, the program requests the new number but does not present the new values, being locked in the loop conditional.

Follows logic used by me:

static void Main(string[] args)
{    
    int cont = 1, valor, auxiliar, continuar = 1;

    while (continuar == 1)
    {    
        Console.WriteLine("Digite um numero de 1 a 10: ");
        valor = int.Parse(Console.ReadLine());    

        if (valor <= 10)
        {
            while (cont <= 10)
            {    
                auxiliar = valor * cont;
                Console.WriteLine(valor + " X " + cont + " = " + auxiliar);    
                cont++;
            }

            Console.WriteLine("Deseja verificar a tabuada de algum outro número? Digite 1 para SIM ou 2 para NAO");
            continuar = int.Parse(Console.ReadLine());
            Console.ReadKey();
        }
        else
        {
            Console.WriteLine("Número inválido! ");
        }
    }
}

2 answers

4

This code is barely readable, has too many variables, and they are declared long before being used which complicates understanding leading to errors.

Actually there are several things that can be improved on it, including it is a clear situation to use a for and not a while.

Since you are doing checks if the typing is correct, do it in full, and if someone types something other than a number? At the moment your code will break. No. just test if the code works. It has to be the one that doesn’t work, this is much more important. Use a TryParse().

I’ve also modernized the code. Most of the material you teach out there is obsolete, so learn to do it the way it’s done today.

It can get better.

using static System.Console;

public class Program {
    public static void Main(string[] args) {    
        int continuar = 1;
        while (continuar == 1) {    
            WriteLine("Digite um numero de 1 a 10: ");
            int valor;    
            if (int.TryParse(ReadLine(), out valor) && valor >= 1 && valor <= 10) {
                for (int cont = 1; cont <= 10; cont++) WriteLine($"{valor:D2} X {cont:D2} = {valor * cont:D2}");
                WriteLine("Deseja verificar a tabuada de algum outro número? Digite 1 para SIM ou 2 para NAO");
                if (int.TryParse(ReadLine(), out continuar)) continuar = 2;
            } else {
                WriteLine("Número inválido! ");
            }
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Would you know some place where I could learn this way used these days?

  • 1

    Nothing in central place, is to follow up, asking here. I’m thinking about writing a book about this.

  • It would be a great contribution to the community, for sure. However, could you explain to me this line: Writeline($"{value:D2} X {cont:D2} = {value * cont:D2}");

  • @Mateusbinatti here does not fit, ask a new question shows this and saying what did not understand.

4


It’s because you start cont outside the while and does not return its value to the initial. This causes the while internal is not executed, since its condition is that the variable cont is less than or equal to 10.

See working on . NET Fiddle.

public static void Main(string[] args)
{
    int cont, valor, auxiliar, continuar = 1;
    while (continuar == 1)
    {
        Console.WriteLine("Digite um numero de 1 a 10: ");
        valor = int.Parse(Console.ReadLine());

        if (valor <= 10)
        {
            cont = 1;
            while (cont <= 10)
            {
                 auxiliar = valor * cont;
                 Console.WriteLine(valor + " X " + cont + " = " + auxiliar);
                 cont++;
            }

            Console.WriteLine("Deseja verificar a tabuada de algum outro número? Digite 1 para SIM ou 2 para NAO");
            continuar = int.Parse(Console.ReadLine());                
        }
        else
        {
            Console.WriteLine("Número inválido! ");
        }
    }
}

Browser other questions tagged

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