我正在尝试获取一个程序来打印带有 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 的一半,但是一旦我得到了这一面,我可以将其反转以生成其余部分。
我会很感激对我的数学有帮助。