从小字符打印大字符

时间:2012-11-11 13:37:51

标签: java

我正在尝试用x打印一个'X'的程序。即:

xxx      xxx
 xxx    xxx
  xxx  xxx
   xxxxxx
  xxx  xxx
 xxx    xxx
xxx      xxx

这是我到目前为止所做的:

import java.util.Scanner;
public class CustomXfactor {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        boolean quit = false;
        int i=0, a=0, c=0;

        System.out.printf("length must be between 16 and 120\n");

        //Ask for desired length
        System.out.printf("Please enter the length (0  to exit):\n");
        int length = in.nextInt();
        int spaces = (length-length+1), //Calculate spaces before the first leg
                innerSpaces = (length-6); //Calculate the inner spaces -6
                                         //because there is 6 Xs which are 
                                         //the form the legs

        while(!quit){
            //Print first half of the X
            for (i=0;i<(length/2);i++){

                //First XXX leg
                System.out.printf("XXX");

                //Space between the legs
                for (a=length-6;a<innerSpaces;a++){
                    System.out.printf(" ");
                }
                //Second XXX leg
                System.out.printf("XXX\n");

                //SPACES 
                for (c=0;c<(spaces);c++){
                System.out.printf(" ");
                }
                spaces++;
                innerSpaces--;
            }
            quit = true; //Change boolean to break while loop
        }//END of while loop
    }//END of method main
}//END end of class CustomXfactor

我的数学问题在第26行。我没有得到循环来打印X的腿之间的正确空格,然后在循环时取走一个。

正如你所看到的,这只是X的一半,但是一旦我站到了这一边,我可以将其反转以产生其余部分。

我很感激那里的数学帮助。

2 个答案:

答案 0 :(得分:4)

分而治之方法将使您的工作更容易理解并且更容易解决。 考虑处理每行尾随空格的打印问题的方法以及“XXX”单位字符串之间的空格。我不想完全解决你的问题,但我认为你应该看看这段代码,以了解你所缺少的东西。使用此代码,您可以在String数组中获得所需输出的一半。然后按顺序遍历它然后以相反的顺序遍历它将为您提供正确的输出。

public static void main(String[] args) {
    String unit = "XXX"; // Unit String.
    int width = 21; // You can get this input from user.
    int betweenSpaces = width - 2 * unit.length(), trailingSpaces = 0;

    String[] lines = new String[(width - 2 * unit.length()) / 2 + 1];

    for (int i = 0; i < lines.length; i++) {
        lines[i] = "";
        lines[i] = helper(trailingSpaces, lines[i], unit)
                 + helper(betweenSpaces, lines[i], unit);

        betweenSpaces -= 2; // Decrement space count by 2.
        trailingSpaces += 1; // Increment trailing space count by 1.
    }

    // Printing lines array.
    for (String str : lines)
        System.out.println(str);
    for (int i = lines.length - 2; i >= 0; i--)
        System.out.println(lines[i]);
}

public static String helper(int count, String ref, String unit) {
    for (int j = 0; j < count; j++)
        ref += " ";
    return ref += unit; // 2nd unit string appended.
}

<强>输出:

XXX               XXX
 XXX             XXX
  XXX           XXX
   XXX         XXX
    XXX       XXX
     XXX     XXX
      XXX   XXX
       XXX XXX
      XXX   XXX
     XXX     XXX
    XXX       XXX
   XXX         XXX
  XXX           XXX
 XXX             XXX
XXX               XXX

答案 1 :(得分:1)

我替换了这个

//Space between the legs
for (a=length-6;a<innerSpaces;a++){
     System.out.printf(" ");
}

//Space between the legs
for (a=length-6;a<innerSpaces;a++){
     System.out.printf("o");
}

并且没有打印o ...

您初始化int innerSpaces = (length-6);所以内容空间(length-6);在您初始化a=length-6的循环中innerSpacesa < innerSpaces完全相同但仅在{{1}}时进入循环。这就是为什么永远不能执行这个循环的原因。