Difference between revisions of "ASCII Art Examples"
Line 16: | Line 16: | ||
<pre> | <pre> | ||
− | + | 0 1 2 3 4 5 6 7 8 9 | |
− | + | +–––––––––––––––––––––––––––––+ | |
− | 0| | + | 0|__|__|__|__|__|__|__|__|__|__| |
− | + | |__|__|__|__|__|__|__|__|__|__| | |
− | 1| | + | 1|__|__|__|__|__|__|__|__|__|__| |
− | + | |__|__|__|__|__|__|__|__|__|__| | |
− | 2| | + | 2|__|__|__|__|__|__|__|__|__|__| |
− | + | |__|__|__|__|__|__|__|__|__|__| | |
− | 3| | + | 3|__|__|__|__|__|__|__|__|__|__| |
− | + | |__|__|__|__|__|__|__|__|__|__| | |
− | 4| | + | 4|__|__|__|__|__|__|__|__|__|__| |
− | + | |__|__|__|__|__|__|__|__|__|__| | |
− | 5| | + | 5|__|__|__|__|__|__|__|__|__|__| |
− | + | |__|__|__|__|__|__|__|__|__|__| | |
− | 6| | + | 6|__|__|__|__|__|__|__|__|__|__| |
− | + | |__|__|__|__|__|__|__|__|__|__| | |
− | 7| | + | 7|__|__|__|__|__|__|__|__|__|__| |
− | + | |__|__|__|__|__|__|__|__|__|__| | |
− | 8| | + | 8|__|__|__|__|__|__|__|__|__|__| |
− | + | |__|__|__|__|__|__|__|__|__|__| | |
− | 9| | + | 9|__|__|__|__|__|__|__|__|__|__| |
</pre> | </pre> | ||
Revision as of 12:41, 6 April 2010
Back to the Program-A-Day homepage
ProblemIn this sample problem, we'll examine how to create a number of different "pictures" using two nested loops and printing characters to the console. These pictures include: a solid box, a solid box with a blank "X", an "X", a hollow box, a hollow box with an "X", and a right triangle. The program will begin by prompting the user for a number. This number will represent the size for each picture that will be printed to the console. In essence, the size will represent the number of rows and columns for each picture. |
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionBefore we can actually start to think about solving these problems, we'll need to a piece of paper and a pencil. Each picture can be represented by a 2-dimensional grid, so let's draw out a 10 x 10 grid on our piece of paper. It may look something like this: 0 1 2 3 4 5 6 7 8 9 +–––––––––––––––––––––––––––––+ 0|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__| 1|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__| 2|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__| 3|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__| 4|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__| 5|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__| 6|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__| 7|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__| 8|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__| 9|__|__|__|__|__|__|__|__|__|__| | ||
CodeSolutionCode goes here. Please DO NOT put your code in <pre> tags! |
import javax.swing.*; // so we can use JOptionPane to get user input public class ASCII_Art { public static void main(String[] args) { String input; // what we'll be using for input... we'll parse each input string to an int int size; // we'll read this value in from the console // this command allows the user to enter a number which will determine the size of our pictures input = JOptionPane.showInputDialog("Enter a number for the size of each picture",null); //now we need to parse our input string to an integer size = Integer.parseInt(input); //solid box //we're just printing the same number of '*'s for each row for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { System.out.print("*"); }//end for System.out.println(); }//end for System.out.println(); //solid box w/ an 'X' for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i == j || j ==(size-1)-i) { System.out.print(" "); }//end if else { System.out.print("*"); }//end else }//end for System.out.println(); }//end for System.out.println(); // an 'X' for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i == j || j ==(size-1)-i) { System.out.print("*"); }//end if else { System.out.print(" "); }//end else }//end for System.out.println(); }//end for System.out.println(); // a hollow box for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if ((i == 0 || i == (size-1)) || (j == 0 || j == (size-1)) ) { System.out.print("*"); }//end if else { System.out.print(" "); }//end else }//end for System.out.println(); }//end for System.out.println(); //Hollow box w/ an 'X' for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i == j || j ==(size-1)-i) { System.out.print("*"); }//end if else if ((i == 0 || i == (size-1)) || (j == 0 || j == (size-1)) ) { System.out.print("*"); }//end else if else { System.out.print(" "); }//end else }//end for System.out.println(); }//end for System.out.println(); //right triangle for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (j <= i) { System.out.print("*"); }//end if else { System.out.print(" "); }//end else }//end for System.out.println(); }//end for System.out.println("*** END OF PROCESSING. ***"); }//end main }//end ASCII_Art