Number of Lines Done Correctly

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:

The program, with a correct outer loop, is below.

Number of Lines Done Correctly

If ( more stuff goes here ) somehow printed a row of numStars stars, the program would be finished.

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() ;

    row  =  1;
    while ( row <= numRows )    
    {

      ( more stuff goes here )

      System.out.println();
      row = row + 1;
    }
  }
}

Now you must perform one of the most important tasks in computer programming: ignoring the big problem and looking at a little problem.

QUESTION 20:

Clear you mind of all thoughts about the big program. Just think about the little problem. How would you print a row of numStars stars, one star at a time?