Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Problem2Example {
- static int N;
- public static void main (String[] args) {
- Scanner myScanner = new Scanner (System.in);
- System.out.println("What dimension do you want in the table?");
- int N = myScanner.nextInt();
- myScanner.nextLine(); // pick up the enter key
- // print header row
- System.out.print(" |");
- for (int i = 1; i <= N; i++)
- System.out.print(pad(i) + " ");
- System.out.println();
- // print separator
- System.out.print("----");
- for (int i = 1; i <= N; i++)
- System.out.print("----");
- System.out.println();
- // print addition table
- for (int i = 1; i <= N; i++) {
- System.out.print(pad(i) + "|");
- for (int j = 1; j <= N; j++) {
- System.out.print(pad(i+j) + " ");
- }
- System.out.println();
- }
- } // end of table
- // pad: add blanks to make it 3 long
- public static String pad(int x) {
- String s = new String();
- if (x < 10) s = " " + x;
- else if (x < 100) s = " " + x;
- else s = "" + x;
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement