TableUtilities.java 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // String.format();
  2. // %n
  3. import java.util.Formatter;
  4. public class TableUtilities {
  5. public static String getSmallMultiplicationTable() {
  6. StringBuilder table = new StringBuilder();
  7. for (int i = 1; i <= 5; i++) {
  8. for (int j = 1; j <= 5; j++) {
  9. table.append(String.format("%3s |", j * i));
  10. }
  11. table.append("\n");
  12. }
  13. //String tableLine = String.format("| %s |", table);
  14. System.out.println(table);
  15. return table.toString();
  16. }
  17. public static String getLargeMultiplicationTable() {
  18. StringBuilder table = new StringBuilder();
  19. for (int i = 1; i <= 10; i++) {
  20. for (int j = 1; j <= 10; j++) {
  21. table.append(String.format("%3s |", j * i));
  22. }
  23. table.append("\n");
  24. }
  25. //String tableLine = String.format("| %s |", table);
  26. System.out.println(table);
  27. return table.toString();
  28. }
  29. public static String getMultiplicationTable(int tableSize) {
  30. StringBuilder table = new StringBuilder();
  31. for (int i = 1; i <= tableSize; i++) {
  32. for (int j = 1; j <= tableSize; j++) {
  33. table.append(String.format("%3s |", j * i));
  34. }
  35. table.append("\n");
  36. }
  37. //String tableLine = String.format("| %s |", table);
  38. System.out.println(table);
  39. return table.toString();
  40. }
  41. }