Question
I just asked this question, however the answer was not what I was looking for, so hopefully this time I can explain it better :)
I just asked this question, however the answer was not what I was looking for, so hopefully this time I can explain it better :)
I have two methods. One that draws a rectangle and one that draws a triangle. The triangle works perfectly, however I am having some issues with the rectangle method.
The rectangle method draws the rectangle fine. The issue Im having is my less than isnt working. If the width or height are less than 1 I would like to return the string wrong, once. However if it is equal to or more than 1 for the method to draw the square.
At the moment my code draws the rectangle if the width or height are greater than 1, not equal to or greater than one. And the first if function does return the correct string however it returns it many times, i want it to return just once, if that makes sense.
I have pasted the code below.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
public class ShapeDrawer {
public static string DrawSquare(int height, int width) { // Write your code to draw the square here string s = ""; string wrong = "Cannot draw that shape.";
if (width < 1 || height < 1) { return wrong; }
else { for (int i = 1; i <= height; i++) { for (int j = 1; j <= width; j++) { if (i == 1 | j == 1 || i == height || j == width) { if (width <= 1 || height <= 1) { s = s + "Cannot draw that shape."; } else { s = s + "-"; } } else { s = s + " "; } } s = s + " "; }
return s; } }
public static string DrawIsoscelesTriangle(int size) { // Write your code to draw the triangle here string s = ""; string wrong = "Cannot draw that shape.";
for (int i = 1; i <= size; i++) { for (int j = 1; j <= i; j++) { s = s + "$"; } s = s + " "; } for (int i = size - 1; i >= 1; i--) { for (int j = 1; j <= i; j++) { s = s + "$"; } s = s + " ";
} if (size < 2) { return wrong; } else { return s; } }
}
class Program { static void Main() { Console.WriteLine(ShapeDrawer.DrawSquare(1, 5)); Console.WriteLine(ShapeDrawer.DrawIsoscelesTriangle(2)); Console.WriteLine(" Hit Enter to exit."); Console.ReadLine(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started