TableUtilities.java 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import java.lang.StringBuilder;
  2. public class TableUtilities {
  3. public static String getSmallMultiplicationTable() {
  4. StringBuilder sb = new StringBuilder();
  5. for (int i = 1; i < 6; i++) {
  6. for (int j = 1; j < 6; j++) {
  7. int product = i*j;
  8. sb.append(" ");
  9. if (product < 10) {
  10. sb.append(" ");
  11. }
  12. sb.append(product + " |");
  13. }
  14. sb.append("\n");
  15. }
  16. return sb.toString();
  17. }
  18. public static String getLargeMultiplicationTable() {
  19. StringBuilder sb = new StringBuilder();
  20. for (int i = 1; i < 11; i++) {
  21. for (int j = 1; j < 11; j++) {
  22. int product = i*j;
  23. sb.append(" ");
  24. if (product < 10) {
  25. sb.append(" ");
  26. } else if (product > 99) {
  27. sb.deleteCharAt(sb.length() - 1);
  28. }
  29. sb.append(product + " |");
  30. }
  31. sb.append("\n");
  32. }
  33. System.out.println(sb.toString());
  34. return sb.toString();
  35. }
  36. public static String getMultiplicationTable(int tableSize) {
  37. StringBuilder sb = new StringBuilder();
  38. for (int i = 1; i < tableSize + 1; i++) {
  39. for (int j = 1; j < tableSize + 1; j++) {
  40. int product = i*j;
  41. sb.append(" ");
  42. if (product < 10) {
  43. sb.append(" ");
  44. } else if (product > 99) {
  45. sb.deleteCharAt(sb.length() - 1);
  46. }
  47. sb.append(product + " |");
  48. }
  49. sb.append("\n");
  50. }
  51. System.out.println(sb.toString());
  52. return sb.toString();
  53. }
  54. }