2

I'm trying to dive deeper into C#, and so I've been experimenting with some code. I'm creating a console application that shows the path of a projectile depending on the angle, and it works great! you can check it out here actually. It's not perfect, but I like it.

My question is, is there a better way to use my for loops? I'm using 3 inside eachother, and I'm not sure if that should be an issue. If you could take a look at the code in the link, it'd be great. Thanks.

EDIT:

I realized my code might not have shown up.

using System.IO;
using System;
using System.Threading;
class Program
{    
    static void Main()
    {
        Console.Clear();         
        DoArc();
    }

    static void DoArc() 
    {        
        int tempVal = 0;
        string displayLine = "";        
        for(int a = 0; a < 90; a+= 2) 
        {
            Console.Clear();
            for(int y = 0; y < 16; y++) 
            {                
                for(int x = 0; x < 40; x++) 
                {
                    tempVal = Mathf.RoundToInt(Physics.HeightAtPoint(x, a, 15f, 0));
                    tempVal = Mathf.Clamp(tempVal, 0, 16);                    
                    if (Mathf.CloseTo(16 - y, tempVal, 1)) 
                        displayLine += " X";
                    else
                        displayLine += " .";                    
                }
                Console.WriteLine(displayLine);
                displayLine = "";
            }
            Console.WriteLine("Angle: " + a);
            Thread.Sleep(500);
        }
    }
}
12
  • 5
    better to ask it on codereview Aug 27, 2015 at 13:41
  • @M.kazemAkhgary, this could be a valid codereview post, assuming that code works.
    – Quill
    Aug 27, 2015 at 13:42
  • 3
    if you need to perform an action x*y*z times, then 3 for loops is pretty much necessary Aug 27, 2015 at 13:45
  • 3
    about the only obvious improvement I can see would be to use a StringBuilder instead of appending all your text into the string displayLine . This would reduce memory costs as strings are immutable. Also, doing displayLine = ""; doesn't actually free up memory. Aug 27, 2015 at 13:47
  • 5
    I'm voting to close this question as off-topic because there is no problem to solve with the code in question.
    – Servy
    Aug 27, 2015 at 13:54

0

Browse other questions tagged or ask your own question.