It is a good practice for a method to validate its input before doing its job. If it is true, print star otherwise, print empty space. The if-else condition check whether it is a rectangle first and last row or column. This Java rectangle star example uses nested for loop to iterate the rectangle rows and columns. After doing a bit of poking around, here are 13 things you will learn about Stars Hollow when you peruse the towns state of the art website. You may end up writing only only Write(): Console.Write(.) Write a Java Program to print hollow rectangle star pattern using for loop. You may avoid repeating code in an if/else block, so instead of: if (.) Depending of your progression in the course, you may also be interested in those aspects: This is hopefully all you need to do the exercise. A second thing you can do is to put the condition in a separate method, which may improve readability if you find a good name for the method: private void DrawRectangle(int width, int height) If it makes sense to regroup a with b and c with d. The first thing is to use intermediary variables. It might be that your final condition will become too difficult to read. The first one means that the condition is true if both operands are true, and the second one means that either the first or the second operand is true. In order to combine conditions, you can use & and || operators. Now you must include the other cases in your condition: the first column, and the last column and row. The essential part of your application can be reduced to: private void DrawFillRectangle(int width, int height) Int32.TryParse(userChoiceR, out iRowMax) įor (int iRow = 0 iRow < iRowMax iRow++)įor (int iCol = 0 iCol < iColMax iCol++) Int32.TryParse(userChoiceC, out iColMax) Ĭonsole.WriteLine("How many rows tall should the rectangle be? ") //i.e. columns are side by side, rows is number of top to bottomĬonsole.WriteLine("Welcome to the HollowRectanglePrinter Program.") Ĭonsole.WriteLine("How many columns wide should the rectangle be?") //i.e. how are now supposed to make this hollow? How to output a hollow rectangle using if logic if possible? class Nested_Loops_Hollow_Rectangles This is what I've done, which displays a simple (filled) rectangle. For instance, the solution on another forum that seems too challenging, and this answer on CodeReview.SE is too difficult to understand. I have been able to make a simple rectangle, but hollow rectangle programs I've looked at either contained or arrays or were pretty convoluted. Could someone advise me on a simple way to implement hollow rectangles in C#?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |