TableUtilities.java 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. public class TableUtilities {
  2. public static String getSmallMultiplicationTable() {
  3. StringBuilder sb = new StringBuilder();
  4. for (int i = 1; i <= 5; i++) {
  5. for (int j = 1; j <= 5; j++) {
  6. sb.append(String.format("%3d |",(i*j)));
  7. }
  8. sb.append("\n");
  9. }
  10. System.out.print(sb);
  11. return sb.toString();
  12. }
  13. public static String getLargeMultiplicationTable() {
  14. StringBuilder sb = new StringBuilder();
  15. for (int i = 1; i <= 10; i++) {
  16. for (int j = 1; j <= 10; j++) {
  17. sb.append(String.format("%3d |",(i*j)));
  18. }
  19. sb.append("\n");
  20. }
  21. System.out.print(sb);
  22. return sb.toString();
  23. }
  24. public static String getMultiplicationTable(int tableSize) {
  25. StringBuilder sb = new StringBuilder();
  26. for (int i = 1; i <= tableSize; i++) {
  27. for (int j = 1; j <= tableSize; j++) {
  28. sb.append(String.format("%3d |",(i*j)));
  29. }
  30. sb.append("\n");
  31. }
  32. System.out.print(sb);
  33. return sb.toString();
  34. }
  35. }