Loop is not repeating as it should

3

I am creating a code that reads the number entered by the user and, if it is from 1 to 10, he / she performs the table, presenting value by value on the screen, and allows the user to choose whether to check another table.

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

Follow the 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! ");
        }
    }
}
    
asked by anonymous 14.07.2017 / 19:51

2 answers

4

It's because you start cont out of while principal and do not return the value of it's initial pro. This causes the internal%% of the% does not execute, since its condition is that the while variable is less than or equal to 10.

See working in .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! ");
        }
    }
}
    
14.07.2017 / 19:58
4

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

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

Since you are checking if typing is correct, do it in full, and if someone types something other than a number? In omentation your code will break. No. Just test if the code works. It has to be what does not work, this is much more important. Use a TryParse() .

I've also modernized the code. Most of the teaching material out there is obsolete, so learn to do it the way you do today.

It can improve.

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! ");
            }
        }
    }
}

See running on .NET Fiddle . And no Coding Ground . Also put it in GitHub for future reference .

    
15.07.2017 / 19:08