Reputation: 75
I am trying to get a program to print a 'X' with x's. ie:
xxx xxx
xxx xxx
xxx xxx
xxxxxx
xxx xxx
xxx xxx
xxx xxx
This is what I have done so far:
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
My math issue is in line 26. I am not getting the loop to print the correct spaces between the legs of the X, and then taking one away as it loops.
As you can see, this is only half of the X, but once I got this side, I can reverse it to generate the rest.
I would appreciate some help on my math there.
Upvotes: 5
Views: 3016
Reputation: 25950
Divide-and-conquer approach will make your work easier to understand and easier to solve. Think of the ways of handling the printing issues of the trailing spaces of each line as well as the spaces between "XXX" unit Strings. I didn't want to solve your problem completely, but I think you should take a look at this code to understand what you are missing. With this code, you get the half of the desired output in a String array. And traversing it in order and then in reverse order will give you the correct output.
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.
}
Output:
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
Upvotes: 4
Reputation: 37813
I replaced this
//Space between the legs
for (a=length-6;a<innerSpaces;a++){
System.out.printf(" ");
}
with
//Space between the legs
for (a=length-6;a<innerSpaces;a++){
System.out.printf("o");
}
and there was no o
printed...
You initilze int innerSpaces = (length-6);
so innerspaces is (length-6);
in the loop you initialze a=length-6
which is the exact same as innerSpaces
but only enter the loop if a < innerSpaces
. That's why this loop can never be executed.
Upvotes: 1