Nesting Loops and Ifs
19. Counting Loop for Lines
Answer:
Yes — if the user asks for five lines, a loop should count 1, 2, 3, 4, 5.
Counting Loop for Lines
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() ; while ( ) { System.out.println( "Row " + row ); // temporary software testing (more statements go here ) } } } |
Some of the information collected from the user is not used yet. First concentrate on looping the correct number of lines.
(As usual, build the program in stages and make sure each stage is correct before building on it.)
Question 19:
Fill in the three blanks. Select from the following:
row > numRows
row <= numRows
row < numRows
row = 0;
row = 1;
row = row + 1;
row = row - 1;
(Not all choices are used.)