TableUtilities.java 758B

1234567891011121314151617181920212223242526
  1. public class TableUtilities {
  2. public static String getSmallMultiplicationTable() {
  3. return getMultiplicationTable(5);
  4. }
  5. public static String getLargeMultiplicationTable() {
  6. return getMultiplicationTable(10);
  7. }
  8. public static String getMultiplicationTable(int tableSize) {
  9. StringBuilder table = new StringBuilder();
  10. for (int i = 1; i <= tableSize; i++) {
  11. StringBuilder row = new StringBuilder();
  12. for (int j = 1; j <= tableSize; j++) {
  13. int number = i * j;
  14. String formatted = String.format("%3d |", number);
  15. row.append(formatted);
  16. }
  17. table.append(row + "\n");
  18. }
  19. return table.toString();
  20. }
  21. }