/** * Name: Erica Eddy * Course: CSCI 241 - Computer Science I * Section: 001 or 002 * Assignment: 1 */ import java.util.*; public class MultiplicationTable { public static void main (String [] args) { // ask user for # of rows and columns int rows = getCount("rows"); int cols = getCount("columns"); // print the header line // add these lines for step 3 System.out.print(" |"); for (int i = 1; i <= 10; i++) System.out.printf("%4d",i); System.out.println(); // print the dashes // add these lines for step 4 System.out.print("---"); for (int i = 0; i < 10; i++) System.out.print("----"); System.out.println(); // call a method to print the table printTable(rows, cols); } // end main() method public static void printTable(int rows, int cols) { // the outer loop controls which row we are printing // remember when we set the outer loop to run from r=1 to r=1, // we printed one row (with all columns) for (int r = 1; r <= rows; r++) { System.out.print(" " + (r) + "|"); // the inner loop runs completely from start to finish, // every time we get a new value for r for (int c = 1; c <= cols; c++) { // format lines for step 2 int product = (c ) * (r); System.out.printf ("%4d",product); } // end inner for loop System.out.println(); } // end outer for loop } // end printTable // ask for # of rows or columns public static int getCount(String which) { int count = 0; Scanner keyboard = new Scanner(System.in); System.out.print("Enter # of " + which + ": "); count = keyboard.nextInt(); return count; } }