How to print 1 to 100 without any looping using C#

后端 未结 28 1233
天命终不由人
天命终不由人 2020-12-22 19:30

I am trying to print numbers from 1 to 100 without using loops, using C#. Any clues?

相关标签:
28条回答
  • 2020-12-22 20:00
    Console.WriteLine('1');
    Console.WriteLine('2');
    ...
    Console.WriteLine('100');
    

    ...Or would you have accepted a recursive solution?

    EDIT: or you could do this and use a variable:

    int x = 1;
    Console.WriteLine(x);
    x+=1;
    Console.WriteLine('2');
    x+=1;
    ...
    x+=1
    Console.WriteLine('100');
    
    0 讨论(0)
  • 2020-12-22 20:03

    No loops, no recursion, just a hashtable-like array of functions to choose how to branch:

    using System;
    using System.Collections.Generic;
    
    namespace Juliet
    {
        class PrintStateMachine
        {
            int state;
            int max;
            Action<Action>[] actions;
    
            public PrintStateMachine(int max)
            {
                this.state = 0;
                this.max = max;
                this.actions = new Action<Action>[] { IncrPrint, Stop };
            }
    
            void IncrPrint(Action next)
            {
                Console.WriteLine(++state);
                next();
            }
    
            void Stop(Action next) { }
    
            public void Start()
            {
                Action<Action> action = actions[Math.Sign(state - max) + 1];
                action(Start);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                PrintStateMachine printer = new PrintStateMachine(100);
                printer.Start();
                Console.ReadLine();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-22 20:04
    class Program
    {
        static Timer s = new Timer();
        static int i = 0;
        static void Main(string[] args)
        {
            s.Elapsed += Restart;
            s.Start();
            Console.ReadLine();
        }
        static void Restart(object sender, ElapsedEventArgs e)
        {
            s.Dispose();
            if (i < 100)
            {
                Console.WriteLine(++i);
                s = new Timer(1);
                s.Elapsed += Restart;
                s.Start();
            }
        }
    }
    

    You must notice that I'm NOT using recursion.

    0 讨论(0)
  • 2020-12-22 20:08

    The cool and funny way:

    static void F(int[] array, int n)
    {
        Console.WriteLine(array[n] = n);
        F(array, n + 1);
    }
    static void Main(string[] args)
    {
        try { F(new int[101], 1); }
        catch (Exception e) { }
    }
    
    0 讨论(0)
  • 2020-12-22 20:09

    By the time I answer this, someone will already have it, so here it is anyway, with credit to Caleb:

    void Main()
    {
        print(0, 100);
    }
    
    public void print(int x, int limit)
    {
        Console.WriteLine(++x);
        if(x != limit)
            print(x, limit);
    }
    
    0 讨论(0)
  • 2020-12-22 20:09
        [Test]
        public void PrintNumbersNoLoopOrRecursionTest()
        {
            var numberContext = new NumberContext(100);
    
            numberContext.OnNumberChange += OnNumberChange(numberContext);
            numberContext.CurrentNumber = 1;
        }
    
        OnNumberChangeHandler OnNumberChange(NumberContext numberContext)
        {
            return (o, args) =>
            {
                if (args.Counter > numberContext.LastNumber)
                    return;
    
                Console.WriteLine(numberContext.CurrentNumber);
    
                args.Counter += 1;
                numberContext.CurrentNumber = args.Counter;
            };
        }
    
    
    public delegate void OnNumberChangeHandler(object source, OnNumberChangeEventArgs e);
    public class NumberContext
    {
        public NumberContext(int lastNumber)
        {
            LastNumber = lastNumber;
        }
    
        public event OnNumberChangeHandler OnNumberChange;
        private int currentNumber;
        public int CurrentNumber
        {
            get { return currentNumber; }
            set {
                currentNumber = value;
                OnNumberChange(this, new OnNumberChangeEventArgs(value));
            }
        }
    
        public int LastNumber { get; set; }
    
        public class OnNumberChangeEventArgs : EventArgs
        {
            public OnNumberChangeEventArgs(int counter)
            {
                Counter = counter;
            }
    
            public int Counter { get; set; }
        }
    
    0 讨论(0)
提交回复
热议问题