简体   繁体   中英

java print text with square/frame of *

I am trying to figure out how to print text wrapped with a 80x80 square of *.

My code so far:

Scanner scanner = new Scanner(System.in);
    String text = scanner.next();

    int square = 80;

    if(square > 0){
        for(int i = 0; i<square; i++){
            for(int j = 0; j<square; j++){
                if(i == 0 || j == 0 || i == square-1 || j == square-1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }

However I cant figure out how to put the text in the square, any ideas? The frame/square must always be 80x80 *'s even if the text is longer.

Try this. Split the input into a character array and print each one.

Scanner scanner = new Scanner(System.in);
String text = scanner.next();
char[] characters = text.toCharArray(); //create character array of letters
int square = 80; //length of box

if (square > 0) {
    int index = 0;
    for (int i = 0; i < square; i++) {
        for (int j = 0; j < square; j++) {
            if (i == 0 || j == 0 || i == square - 1 || j == square - 1)
                System.out.print("*");
            else {
                if (index < characters.length && index < square * square) //if index in bounds
                    System.out.print(characters[index++]); //print next letter
                else {
                    System.out.print(" "); //else whitespace
                }
            }
        }
        System.out.println();
    }
}

Alternative approach. More lines of code, but less convulted imho.

static int frameSize = 80;

    public static void printTopOrBottom() {
        char[] topAndBot = new char[frameSize];
        Arrays.fill(topAndBot, '*');
        System.out.println(topAndBot);
    }

    public static char[] createLine() {
        char[] tmpArr = new char[frameSize];
        Arrays.fill(tmpArr, ' ');
        tmpArr[0] = '*';
        tmpArr[frameSize-1] = '*';
        return tmpArr;
    }

    public static char[] getInputText() {
        char[] lotsOfText = new char[ (int)(Math.random() * (160*160))];
        Arrays.fill(lotsOfText, 'r');
        return lotsOfText;
    }

    public static void main(String[] args) {
        char[] input = getInputText();

        ArrayList<char[]> lines = new ArrayList<char[]>();

        int lettersPrinted = 0;
        for(int i = 1; i < (frameSize-1); i++) {
            char[] tmpArr = createLine();
            // find out how many letters on line i, - max 78, but less if no input text left.
            int lettersToPrint = Math.min((input.length - lettersPrinted), (frameSize-2));
            // if any?
            if(lettersToPrint > 0) {
                System.arraycopy(input, lettersPrinted, tmpArr, 1, lettersToPrint);
            }
            lettersPrinted += lettersToPrint;
            // add line
            lines.add(tmpArr);
        }
        printTopOrBottom();
        for(char[] line : lines) {
            System.out.println(line);
        }
        printTopOrBottom();
    }

Here's another one, using printf . Its 3 times faster than the choosen answer.

public static void main(String[] args) {
long start = System.currentTimeMillis();
int intSquare = 80;
int textLength = intSquare - 2;
String square = String.valueOf(textLength);
String textLine = "Tess is very beautiful. Tess is very beautiful. Tess is very beautiful. Tess is very beautiful. Tess is very beautiful. ";
String toPrint = null;
for (int i = 0; i < intSquare; i++) {
    System.out.print("*");
}
System.out.println();
for (int i = 0; i < textLength; i++) {
    toPrint = textLine.length() > textLength ? textLine.substring(0, textLength) : textLine;
    textLine = toPrint.length() == textLength ? textLine.substring(textLength) : "";
    System.out.printf("*%-" + square + "s*\n", toPrint);
}
for (int i = 0; i < intSquare; i++) {
    System.out.print("*");
}
long end = System.currentTimeMillis();
System.out.println();
System.out.println(end - start);
}

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