import java.util.Scanner;
public class AsciiSquare
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int side = sc.nextInt();
for(int i = 0; i<side; i++)
{
for(int k = 0; k<side; k++)
{
if(i == 0 || k = 0 || i == (side-1) || k == (side-1))
System.out.print("*");
}
System.out.println("");
}
}
}
Refactorings
No refactoring yet !
Xerothermic
May 18, 2008, May 18, 2008 18:13, permalink
import java.util.Scanner;
public class AsciiSquare
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int side = sc.nextInt();
for(int i = 0; i<side; i++)
{
for(int k = 0; k<side; k++)
{
if(i == 0 || k == 0 || i == (side-1) || k == (side-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
Jeremy Weiskotten
May 21, 2008, May 21, 2008 19:10, permalink
Here's a different approach. Should be more efficient, if that matters.
import java.util.Scanner;
public class AsciiSquare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int side = sc.nextInt();
char[] edge = new char[side];
char[] innerSpace = new char[side - 2];
java.util.Arrays.fill(edge, '*');
java.util.Arrays.fill(innerSpace, ' ');
String row = "*" + new String(innerSpace) + "*";
System.out.println(edge);
for (int i = 0; i < side - 2; i++) {
System.out.println(row);
}
System.out.println(edge);
}
}
Daniel Becker
May 24, 2008, May 24, 2008 14:52, permalink
I didn't run any of these through the profiler so I can't be 100% certain these changes will be more efficient.
Starting with Jeremy's code above I made the following changes:
1. Arrays.fill is actually just a for loop (with a range check) in the standard library, I merged them in to one loop with out any range checks.
2. Rather than printing out as a char[] I'm printing as a String.
3. I removed the subtraction operation from the printing for loop.
I strongly suspect that the System.out calls are so slow that everything else is immaterial any way :-) It would probably be faster to write to a string buffer and output it in one go. The print methods flush and have synchronised blocks.
Daniel
import java.util.Scanner;
public class AsciiSquare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int side = sc.nextInt();
char[] edge = new char[side];
char[] row = new char[side];
for (int i=0; i<side; i++){
edge[i] = '*';
row[i] = ' ';
}
//Edges
row[0]='*';
row[edge.length-1]='*';
//Create the strings once
String edgeString = new String(edge);
String rowString = new String(row);
//Output
System.out.println(edgeString);
for (int i = 2; i < side; i++) {
System.out.println(rowString);
}
System.out.println(edgeString);
}
}
Maros
May 31, 2008, May 31, 2008 21:09, permalink
Quite readable 15 line way of doing it.
import java.util.Scanner;
public class AsciiSquare {
public static void main(String[] args) {
int side = new Scanner(System.in).nextInt();
for (int y = 1; y <= side; ++y, System.out.println())
for (int x = 1; x <= side; ++x)
System.out.print(isBorder(y, x, side) ? '*' : ' ');
}
private static boolean isBorder(int x, int y, int side) {
return x == 1 || y == 1 || x == side || y == side;
}
}
Simply prints a square with the side of user given argument.