Java Reference
In-Depth Information
LISTING 6.4
//********************************************************************
// Stars.java Author: Lewis/Loftus
//
// Demonstrates the use of nested for loops.
//********************************************************************
public class Stars
{
//-----------------------------------------------------------------
// Prints a triangle shape using asterisk (star) characters.
//-----------------------------------------------------------------
public static void main (String[] args)
{
final int MAX_ROWS = 10;
for ( int row = 1; row <= MAX_ROWS; row++)
{
for ( int star = 1; star <= row; star++)
System.out.print ("*");
System.out.println();
}
}
}
OUTPUT
*
**
***
****
*****
******
*******
********
*********
**********
objects are considered to be iterators, which have hasNext and next methods
to process each item from a group. If an object has implemented the Iterable
interface, then we can use a variation of the for loop to process items using a
simplified syntax.
Search WWH ::




Custom Search