go to previous page   go to home page   go to next page hear noise highlighting

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;
//
public 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.

Breaking a big problem into several little problems and solving the little problems is called divide and conquer. It is (perhaps) the most important skill in programming. Often it is not clear how to break a big problem into smaller problems. Unfortunately, all too often programmers proceed without doing this and produce "spaghetti code" — a tangled mess of statements with no clear logic.

Wikipedia on Spaghetti Code


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?


go to previous page   go to home page   go to next page