Difference between revisions of "Nested Loops Answers 1"

From CompSciWiki
Redirect page
Jump to: navigation, search
(Redirecting to Loops Solutions)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
<pre>
+
#REDIRECT[[Loops Solutions#Ascii Art X]]
/**********************************
+
* NestedX:
+
* Prints out a fancy "ascii art"
+
* picture in the shape of an X where
+
* the user supplies the size of the X.
+
***********************************/
+
 
+
import javax.swing.*;
+
 
+
public class NestedX
+
{
+
public static void main(String[] args)
+
{
+
int size = 0;
+
int i, j;
+
 
+
// Keep asking for input until it is greater or equal to 3.
+
while(size < 3)
+
{
+
size = Integer.parseInt(JOptionPane.showInputDialog("Enter a number greater or equal to 3"));
+
}
+
 
+
for(i = 0; i < size; i++)
+
{
+
for (j = 0; j < size; j++)
+
{
+
if(i == j)
+
{
+
System.out.print("*");
+
}
+
else if(j == (size - (i + 1)))
+
{
+
System.out.print("*");
+
}
+
else
+
{
+
System.out.print(" ");
+
}
+
}
+
System.out.println();
+
}
+
 
+
System.out.println("\n~~~End of processing~~~");
+
System.exit(0);
+
 
+
}// end main
+
}// end NestedX
+
</pre>
+
 
+
'''[[nested loops|Back to Nested Loops]]'''
+

Latest revision as of 19:59, 15 July 2007