从小字符打印大字符

use*_*995 5 java

我试图让一个程序用x打印'X'.即:

xxx      xxx
 xxx    xxx
  xxx  xxx
   xxxxxx
  xxx  xxx
 xxx    xxx
xxx      xxx
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

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

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

Juv*_*nis 4

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

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.
}
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)