Counting Loop for Lines

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Yes — if the user asks for five lines, a loop should count 1, 2, 3, 4, 5.

Counting Loop for Lines

Here is the skeleton program, with the usual blanks. The skeleton focuses on getting the number of lines right, and leaves the rest for later.

import java.util.Scanner;
//
class StarBlock
{
  public static void main (String[] args ) 
  {
    Scanner scan = new Scanner( System.in );
    int numRows;      // the number of Rows
    int numStars;     // the number of stars per row
    int row;          // current row number

    // collect input data from user
    System.out.print( "How many Rows? " );
    numRows = scan.nextInt() ;

    System.out.print( "How many Stars per Row? " );
    numStars = scan.nextInt() ;

    
    
    while (  )    
    {

       (more statements go here )

           
    }
  }
}

Some of the information collected from the user is not used yet. First concentrate on looping the correct number of times.

QUESTION 19:

Fill in the three blanks. Select from the following:

row <= numRows
row  =  1;
row = row + 1;