Search code examples
c#for-loopuser-input

how to generate for loop


Hello had a pretty simple problem, I'm trying to create a console program in C# that gets 3 inputs from the user. The start, stop and number of steps.

It's supposed to be a for loop but I don't really get how I can put user input in the for loop, I tried making int's of the user input and then placing the names of the int's in the for loop but it's giving me errors.

The program is supposed to look like the following program in "Ovning 1" site is in Swedish but I hope you guys will get it, tried searching the site but there was never an explanation given. http://csharpskolan.se/showarticle.php?id=119

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

namespace ovning12
{
class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("Mata in Start");
        int startNummer = int.Parse(Console.ReadLine());

        Console.WriteLine("Mata in Stop");
        int stopNummer = int.Parse(Console.ReadLine());

        Console.WriteLine("Mata in Steg");
        int stegNummer = int.Parse(Console.ReadLine());


        for (int startNummer; startNummer >= stopNummer; startNummer + stegNummer) 
        {
            Console.WriteLine();


        }
    }
}
}

Solution

  • static void Main(string[] args)
        {
            Console.WriteLine("Mata in Start");
            int startNummer = int.Parse(Console.ReadLine());
    
            Console.WriteLine("Mata in Stop");
            int stopNummer = int.Parse(Console.ReadLine());
    
            Console.WriteLine("Mata in Steg");
            int stegNummer = int.Parse(Console.ReadLine());
    
    
            for (int n = startNummer; n < stopNummer; n += stegNummer)
            {
                Console.Write(n + " ");
               Console.Write("{0} ", n); //(Alternative)
            }
        }