Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Inspection of the route the code takes, sees you go cutEnds times into the first branch of the if, biggestOddNumber() - 2 * cutEnds, the second branch, and cutEnds into the first branch again. Let's see what we can do about that. (I just googled 'C# multiply string, and found http://stackoverflow.com/a/533204 https://stackoverflow.com/a/533204, which is relevant).

Inspection of the route the code takes, sees you go cutEnds times into the first branch of the if, biggestOddNumber() - 2 * cutEnds, the second branch, and cutEnds into the first branch again. Let's see what we can do about that. (I just googled 'C# multiply string, and found http://stackoverflow.com/a/533204, which is relevant).

Inspection of the route the code takes, sees you go cutEnds times into the first branch of the if, biggestOddNumber() - 2 * cutEnds, the second branch, and cutEnds into the first branch again. Let's see what we can do about that. (I just googled 'C# multiply string, and found https://stackoverflow.com/a/533204, which is relevant).

Source Link

This loop looks a bit funny to me:

 int cutEnds = biggestOddNumber() / 2;
 for (int i = 0; i < height; i++)
 {
 for (int j = 1; j <= biggestOddNumber(); j++)
 {
 String singleElement = "*";
 if (j <= cutEnds || j > (biggestOddNumber()-cutEnds))
 {
 pyramid = pyramid + " ";
 }
 else
 {
 pyramid = pyramid + singleElement;
 }
 if (j == biggestOddNumber())
 {
 pyramid = pyramid + "\r\n";
 }
 }
 cutEnds--;
 }

Importantly, the last check: if (j == biggestOddNumber()) means that this operation can (and should) be after the if.

 int cutEnds = biggestOddNumber() / 2;
 for (int i = 0; i < height; i++)
 {
 for (int j = 1; j <= biggestOddNumber(); j++)
 {
 String singleElement = "*";
 if (j <= cutEnds || j > (biggestOddNumber()-cutEnds))
 {
 pyramid = pyramid + " ";
 }
 else
 {
 pyramid = pyramid + singleElement;
 }
 }
 pyramid = pyramid + "\r\n";
 cutEnds--;
 }

As somebody rightfully pointed out, the cutEnds-- can be inside the same loop definition as int i.

 int cutEnds = biggestOddNumber() / 2;
 for (int i = 0; i < height; i++, cutEnds--)
 {
 for (int j = 1; j <= biggestOddNumber(); j++)
 {
 String singleElement = "*";
 if (j <= cutEnds || j > (biggestOddNumber()-cutEnds))
 {
 pyramid = pyramid + " ";
 }
 else
 {
 pyramid = pyramid + singleElement;
 }
 }
 pyramid = pyramid + "\r\n";
 }

Now, one funny thing which may take some time to realise, but cutEnds == height - 1. This gives us a possibility to remove i.

 int cutEnds = biggestOddNumber() / 2;
 for (int cutEnds = biggestOddNumber() / 2; cutEnds >= 0; cutEnds--)
 {
 for (int j = 1; j <= biggestOddNumber(); j++)
 {
 String singleElement = "*";
 if (j <= cutEnds || j > (biggestOddNumber()-cutEnds))
 {
 pyramid = pyramid + " ";
 }
 else
 {
 pyramid = pyramid + singleElement;
 }
 }
 pyramid = pyramid + "\r\n";
 }

Inspection of the route the code takes, sees you go cutEnds times into the first branch of the if, biggestOddNumber() - 2 * cutEnds, the second branch, and cutEnds into the first branch again. Let's see what we can do about that. (I just googled 'C# multiply string, and found http://stackoverflow.com/a/533204, which is relevant).

 for (int cutEnds = height - 1; cutEnds >= 0; cutEnds--)
 {
 pyramid = pyramid + new string(' ', cutEnds);
 pyramid = pyramid + new string('*', biggestOddNumber() - 2 * cutEnds);
 pyramid = pyramid + new string(' ', cutEnds);
 pyramid = pyramid + "\r\n";
 }

(Some languages also allow an easy method to center strings, which would be useful, but C# apparently does not (from quick google search).

(Disclaimer: I did not test this)

lang-cs

AltStyle によって変換されたページ (->オリジナル) /