程序方法,文字包围

时间:2014-11-02 04:06:32

标签: java

     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();
        }
    }
}

我必须让程序使用一个接受字符串的方法,并在屏幕上打印水平居中的字符串,该字符串被一个比消息长六个字符的框所包围。
限制弦的长度以确保它适合屏幕。它没有做我想做的事情,行不匹配,它不止一次显示单词(取决于高度)。

2 个答案:

答案 0 :(得分:1)

我希望这会对你有帮助..

根据您的要求更改此代码:)

   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);

            }
        }

    }

答案 1 :(得分:0)

修改后的代码。您的原始代码效率不高(循环 - 需要更长时间),这更简单。解释是在合作中:

public static void main(String[] args) {
    String ttc; // ttc - short for text to center.
    char ch;

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the text to center: ");
    ttc=sc.nextLine();
    System.out.println("Enter the border character : ");
    ch = sc.next().charAt(0);
    System.out.println();
    textbox(ttc,ch);
}

public static void textbox (String ttc, char ch)
{
    // Size of word to center in middle.
    int textSize = ttc.length();
    // Total width of box is word size plus 12 plus 2 for border.
    int width = textSize + 14;

    // This is the top and bottom of the box. Uses a char array the
    // size of the width the box and replaces null values (\0) with
    // the character specified for the border.
    String topBottom = new String(new char[width]).replace('\0', ch);
    String middle = ch + "      " + ttc + "      " + ch;

    // Print out box.
    System.out.println(topBottom);
    System.out.println(middle);
    System.out.println(topBottom);
}