patternjavaMinor
Java Cross with characters
Viewed 0 times
crosscharacterswithjava
Problem
I managed to make a perfectly even cross with
I'm trying to make this cross easier to code. I have to keep it with
for loops.I'm trying to make this cross easier to code. I have to keep it with
for loops, but it's too long to implement.public class CroixDemoAmmeliore {
public static void main(String args[]) {
int e = 5;
int i;
int j;
int k;
int l;
for (k = 1; k <= e; k++) {
for (i = 1; i <= e; i++) {
System.out.print(" ");
}
for (j = 1; j <= e; j++) {
System.out.print("*");
}
System.out.println();
}
for (k = 1; k <= e; k++) {
for (i = 1; i <= e * 3; i++) {
System.out.print("*");
}
System.out.println();
}
for (k = 1; k <= e; k++) {
for (i = 1; i <= e; i++) {
System.out.print(" ");
}
for (j = 1; j <= e; j++) {
System.out.print("*");
}
System.out.println();
}
}
}Solution
First thing I would say is that you could reduce the number of variables you use. The iterator of a For loop, if instanced from outside the loop's context (as in your case), can be reused. Doing so you could already reduce the numbers of variables you use.
I do not understand what you mean by "easier to code", but if you mean that you seek a way to more easily re-use this code, you could either make a static method that would display a cross with specific parameters or an object with a display function.
Here is an exemple of a Cross object:
Or in the format of a static method (which is actually the display method of the last object without the comments):
Here is how you would use either of these solutions:
Now, if it were not for the necessity of using For loops, I would have replaced the loop that adds the space with a simpler:
and then I would have added the characters of the cross in a loop.
I hope this helps you.
I do not understand what you mean by "easier to code", but if you mean that you seek a way to more easily re-use this code, you could either make a static method that would display a cross with specific parameters or an object with a display function.
Here is an exemple of a Cross object:
public class Cross {
private int sectionSize;
public Cross(int sectionSize) {
this.sectionSize = sectionSize;
}
public void display() {
final int height = sectionSize * 3;
final char character = '*';
//The iterators
int i, j;
//This loop controls the line
for (i = 0; i sectionSize * 2) {
//Add spaces
for (j = 0; j < sectionSize; j++)
System.out.print(" ");
//Add the characters
for (j = 0; j < sectionSize; j++)
System.out.print(character);
}
else
for (j = 0; j < height; j++)
System.out.print(character);
System.out.println();
}
}
}Or in the format of a static method (which is actually the display method of the last object without the comments):
public static void drawCross(int sectionSize, char character) {
final int height = sectionSize * 3;
int i, j;
for (i = 0; i sectionSize * 2) {
for (j = 0; j < sectionSize; j++)
System.out.print(" ");
for (j = 0; j < sectionSize; j++)
System.out.print(character);
}
else
for (j = 0; j < height; j++)
System.out.print(character);
System.out.println();
}
}Here is how you would use either of these solutions:
public static void main(String [] args) {
final int sectionSize = 5;
//Use of the Object
Cross cross = new Cross(sectionSize);
cross.display();
//Use of the static method
Cross.drawCross(sectionSize, '*');
}Now, if it were not for the necessity of using For loops, I would have replaced the loop that adds the space with a simpler:
System.out.printf(("%," + sectionSize + "s"), "");and then I would have added the characters of the cross in a loop.
I hope this helps you.
Code Snippets
public class Cross {
private int sectionSize;
public Cross(int sectionSize) {
this.sectionSize = sectionSize;
}
public void display() {
final int height = sectionSize * 3;
final char character = '*';
//The iterators
int i, j;
//This loop controls the line
for (i = 0; i < height; i++) {
//Check wether or not to add spaces
if (i < sectionSize || i > sectionSize * 2) {
//Add spaces
for (j = 0; j < sectionSize; j++)
System.out.print(" ");
//Add the characters
for (j = 0; j < sectionSize; j++)
System.out.print(character);
}
else
for (j = 0; j < height; j++)
System.out.print(character);
System.out.println();
}
}
}public static void drawCross(int sectionSize, char character) {
final int height = sectionSize * 3;
int i, j;
for (i = 0; i < height; i++) {
if (i < sectionSize || i > sectionSize * 2) {
for (j = 0; j < sectionSize; j++)
System.out.print(" ");
for (j = 0; j < sectionSize; j++)
System.out.print(character);
}
else
for (j = 0; j < height; j++)
System.out.print(character);
System.out.println();
}
}public static void main(String [] args) {
final int sectionSize = 5;
//Use of the Object
Cross cross = new Cross(sectionSize);
cross.display();
//Use of the static method
Cross.drawCross(sectionSize, '*');
}System.out.printf(("%," + sectionSize + "s"), "");Context
StackExchange Code Review Q#54376, answer score: 2
Revisions (0)
No revisions yet.