0

I have a method that draws a triangle using a loop that prints to the console each line. I require to change the modifiers to public static string instead of public static void so I require a return type of string. How could I manipulate my code so that the method returns a multi-lined string of the drawn shape?

1
  • Can you return string[]? If so I can help you. Commented Mar 22, 2018 at 0:10

1 Answer 1

1

Investigate System.Text.StringBuilder. Create one at the start of your method, then instead of Console.Write call yourStringBuilderInstance.Append(). At the end of your method, return yourStringBuilderInstance.ToString().

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your reply! I'm receiving: there is no argument given that corresponds to the required formal parameter 'length' from the stringBuilder.ToString(" ");
I said yourStringBuilderInstance.ToString() not .ToString(" "). If you get an error you have to add your code to your question.
You should use a descriptive name like triangle, not stringBuilder, and .AppendLine() instead of WriteLine(). And throw an ArgumentException instead of an error message so your caller will know it got an error instead of a triangle.
Understood. It's currently printing one big singular line as opposed to the shape. I suppose the string should be in some sort of multi-lined format?
I don't know what “singular lines” are; you have to post what you have to your question because I can't see your screen. Your question still has .Append() where I told you to use .AppendLine().
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.