5

我正在尝试获取一个程序来打印带有 x 的“X”。IE:

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 的一半,但是一旦我得到了这一面,我可以将其反转以生成其余部分。

我会很感激对我的数学有帮助。

4

2 回答 2

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
于 2012-11-11T14:31:13.387 回答
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这与 if 完全相同,innerSpaces但只进入循环a < innerSpaces。这就是为什么这个循环永远无法执行的原因。

于 2012-11-11T13:54:37.173 回答