Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Problem2Example {
  3.  
  4. static int N;
  5.  
  6. public static void main (String[] args) {
  7. Scanner myScanner = new Scanner (System.in);
  8. System.out.println("What dimension do you want in the table?");
  9. int N = myScanner.nextInt();
  10. myScanner.nextLine(); // pick up the enter key
  11.  
  12. // print header row
  13. System.out.print(" |");
  14. for (int i = 1; i <= N; i++)
  15. System.out.print(pad(i) + " ");
  16. System.out.println();
  17.  
  18. // print separator
  19. System.out.print("----");
  20. for (int i = 1; i <= N; i++)
  21. System.out.print("----");
  22. System.out.println();
  23.  
  24. // print addition table
  25. for (int i = 1; i <= N; i++) {
  26. System.out.print(pad(i) + "|");
  27. for (int j = 1; j <= N; j++) {
  28. System.out.print(pad(i+j) + " ");
  29. }
  30. System.out.println();
  31. }
  32. } // end of table
  33.  
  34. // pad: add blanks to make it 3 long
  35. public static String pad(int x) {
  36. String s = new String();
  37. if (x < 10) s = " " + x;
  38. else if (x < 100) s = " " + x;
  39. else s = "" + x;
  40. return s;
  41. }
  42.  
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement