How to show all the numbers of a loop at once in C#?

Asked

Viewed 246 times

0

I’m seeing loop loop on C#, but i always need to press Space Bar to display each loop number, for example by typing the number 100 and need to press 100 times the Space Bar to show an increasing number up to the 100 And that’s exhausting, someone knows how to show all the numbers of a repeat loop in C# at once?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TesteLaco
{
    class Program3
    {
        static int Main(string[] args)
        {
            int numero, i = 1;
            Console.Write("Digite um número: ");
            numero = Convert.ToInt32(Console.ReadLine());
            for (i = 1; i <= numero; i++)
            {
                Console.WriteLine($"O número é: {i}");
                Console.ReadKey();
            }
            return 0;
        }
    }
}
  • Look at the code and follow its execution. You will easily understand which line makes you need to click on a key for it to proceed.

  • As answered in previous question, your code remains problematic and if you don’t start changing you will have problems for the rest of your life.

1 answer

4


Just change the Console.Readkey(); out of the for, under it. So he’ll print out all the numbers and then he’ll wait for the key.

        for (i = 1; i <= numero; i++){

            Console.WriteLine($"O número é: {i}");

        }
        Console.ReadKey();
  • It worked, thank you.

Browser other questions tagged

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