简体   繁体   中英

Printing a rectangle with symbols in JAVA

I have been tasked with the assignment of printing a rectangle. The user will input length, width, and a symbol of their choice to be printed. From this the rectangle will be outputted. This portion of the assignment is done. My next task is to print a rectangle from negative values which will only print an outline of the rectangle (inside area is blank). I am having difficulty creating an algorithm for which to use in this situation.

Any help would be appreciated.

Cheers.

   import java.util.Scanner;

    public class Rectangle
{
public static void main(String[] args)
{

    boolean loopExecuted;
    String userContinue = "NO";

    // Loop to ask to do another rectangle
    do 
    {

    // Variables
    int length;
    int width;
    String symbol;
    char symbolz;
    int xLength;
    int yWidth;


                    // Name Scanner
    Scanner input = new Scanner(System.in);

                    // What program will do
    System.out.println("This program will print a rectangle with the symbol of your choice.  If you enter a positve integer the rectangle"
            + "will be filled, if you enter a negative integer, it will print the outline of the rectangle.\n");


                    // Ask for user input and check for validity

        do 
        {
           System.out.println("Please enter the length of the rectangle that is less than or equal to -10 and less than or equal to 10.");
           length = input.nextInt();
        }
        while (length < -10 || length > 10 || length == 0);

        do
        {
           System.out.println("Please enter the width of the rectangle that is less than or equal to -10 and less than or equal to 10.");
           width = input.nextInt();
        }
        while (width < -10 || width > 10 || width == 0);


        System.out.println("Please enter the symbol to be used for the rectangle");
        symbol = input.next();
        symbolz = symbol.charAt(0);

        System.out.println("You have entered the following values for length, width, and symbol: " + length + " ," + width + " ," + symbolz +"\n");


                 // Algorithm to print filled in rectangle.

        for (yWidth = 1; yWidth <= width; yWidth++) {
            for (xLength = 1; xLength <= length; xLength++) {
                System.out.print(symbolz);
            }

            System.out.println(" ");
        }

                // Algorithm to print outline of rectangle

        //TODO 
        for(yWidth = 1; yWidth >= width; yWidth--){
            for (xLength = 1; xLength >= length; xLength--){
                System.out.print(symbolz);
            }

            System.out.println(" ");
        }


                    // Repeat the program   

     loopExecuted = false;  
   do
           {
               if (!loopExecuted)
               {
                    System.out.println("\n\nWould you like to continue? Please either Yes or No");
               }
               else
               {
                   System.out.println("Please enter a valid response (Yes / No)");
               }
               userContinue = input.next();
               userContinue = userContinue.toUpperCase();
               loopExecuted = true;
           }

   while (!"YES".equals(userContinue) && !"NO".equals(userContinue));

   // Case Insensitive

    userContinue = userContinue.toUpperCase();

    } 
    while (userContinue.equals("YES"));

    }
   }

I'll answer you're question but first I'll start by making a suggestion: use functions! Break your one big main into functions. Breaking code into smaller functions helps to keep it more understandable and easier to maintain (especially to find stuff). Everywhere you use a comment would be a good function. For example, getInput(), printFilledRectangle(int width, int height, char symbol), etc.

Now for your question:

Think about the rules for printing a non-filled rectangle. It seems to me that there are three:

1) If this is the first row, print the symbol n-times (where n = width) 2) If this is the last row, print the symbol n-times 3) Else print one symbol, print n-2 spaces, and then print another symbol

So now incorporate these rules into your non-filled loops. Hope that helps.

EDIT:

Ok - well I can get you started

 for (row = 0; row < height; row++)
     if (row == 0 || row == height - 1)  // first or last row
        // print your symbol n times, where n = width
     else // otherwise, this is an "internal" row
        // print 1 symbol, n-2 spaces, then 1 symbol again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM