Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

     public static void main(String[] args) {
        String r;
        int w;
        int h;
        char c;

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the text: ");
        r=sc.nextLine();
        System.out.println("Enter the character : ");
        c = sc.next().charAt(0);
        System.out.println("Enter your width: ");
        w=sc.nextInt();
        System.out.println("Enter your height: ");
        h=sc.nextInt();
        System.out.println();
        textbox(c,r,w,h);
    }

    public static void textbox (char c, String r, int w, int h)
    {
        String middle = c + "  " + r + "  " + c;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (i == 0 || i == h-2) {
                    System.out.print(c);
                } else if (j ==w-1) {
                    System.out.print(middle);
                } 
            }
            System.out.println();
        }
    }
}

I have to make the program use a method that accepts a string and prints the string centered horizontally on the screen surrounded by a box six characters longer than the message.
Limit the length of the string to ensure it will fit on the screen. It doesn't do what I want it to do, the rows don't match and it displays the word more than once(depending on the height).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
173 views
Welcome To Ask or Share your Answers For Others

1 Answer

I hope this will help you..

Change this code as per your requirement :)

   import java.util.Scanner;

    public class TextBox {
        public static void main(String[] args) {
            String inputString;
            char character;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the text: ");
            inputString=sc.nextLine();
            System.out.println("Enter the character : ");
            character = sc.next().charAt(0);
            sc.close();
            drawTextbox(character,inputString);
        }
        public static void drawTextbox (char character, String inputString){
            int width=inputString.length()+6;
            for(int i=0;i<width;i++){
                System.out.print(character);    
            }
            System.out.println();
            for(int i=0;i<width;i++){

                if(i==0||i==width-1){
                    System.out.print(character);
                }
                else if(i>2&&i<width-3){
                    System.out.print(inputString.charAt(i-3));
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
            for(int i=0;i<width;i++){
                System.out.print(character);

            }
        }

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...