none
code stops running half way through. RRS feed

  • Question

  • hello, i recently got into coding in C#. i was trying to make a program that counts up to 600 using a while loop and a for loop. whilst it counts up to three hundred, i want it to get slower and slower, and then once it gets over three hundred, it is supposed to speed up again. after trying to run the code, i have encountered a problem. after it gets to 371, it stops completely. here is the code:

    static void Main(string[] args)
            {
                double b = 10;
                int a, c;
                while (true)
                {
                    for (int i = 0; i < 600; i++)
                    {

                        if (i <= 300)
                        {
                            b = b + 0.2;
                        }
                        else
                        {
                            b = b - 1;
                        }
                        Console.WriteLine("{0}\t", i);
                        a = Convert.ToInt32(b);
                        
                        System.Threading.Thread.Sleep(a);
                        
                    }
                    break;
                    Console.ReadLine();

                }
            }

    anyone know how to fix it? thanks.

    Wednesday, November 21, 2018 10:07 PM

Answers

  • Because a is -1 

    try this:

            static void Main(string[] args)
            {
                double b = 10;
                int a, c;
                while (true)
                {
                    for (int i = 0; i < 600; i++)
                    {
    
                        if (i <= 300)
                        {
                            b = b + 0.2;
                        }
                        else
                        {
                            b = b - 1;
                        }
                        Console.WriteLine("{0}\t", i);
                        a = Convert.ToInt32(b);
    
                        if (a < 0)
                            a *= -1;
                        System.Threading.Thread.Sleep(a);
    
                    }
                    break;
                    Console.ReadLine();
    
                }
            }

    • Marked as answer by fiachra1997 Wednesday, November 21, 2018 10:42 PM
    Wednesday, November 21, 2018 10:15 PM

All replies

  • Because a is -1 

    try this:

            static void Main(string[] args)
            {
                double b = 10;
                int a, c;
                while (true)
                {
                    for (int i = 0; i < 600; i++)
                    {
    
                        if (i <= 300)
                        {
                            b = b + 0.2;
                        }
                        else
                        {
                            b = b - 1;
                        }
                        Console.WriteLine("{0}\t", i);
                        a = Convert.ToInt32(b);
    
                        if (a < 0)
                            a *= -1;
                        System.Threading.Thread.Sleep(a);
    
                    }
                    break;
                    Console.ReadLine();
    
                }
            }

    • Marked as answer by fiachra1997 Wednesday, November 21, 2018 10:42 PM
    Wednesday, November 21, 2018 10:15 PM
  • thank you, this worked perfectly
    Wednesday, November 21, 2018 10:31 PM
  • I'm glad to her that :). It would be great, if you mark the post with the code as answer.

    Greetings, Chris

    Wednesday, November 21, 2018 10:34 PM