如何在Java中使用嵌套循环打印数学乘法表,以2×2和3×3矩阵形式返回字符串值

huangapple go评论116阅读模式
英文:

How to print math multiplication table returning String value in 2x2 and 3x3 matrix using nested loops in Java

问题

  1. public String simpleMultiplicationTable(int num) {
  2. StringBuilder output = new StringBuilder();
  3. for (int i = 1; i <= num; i++) {
  4. for (int j = 1; j <= num; j++) {
  5. output.append(String.format("%4d", i * j));
  6. }
  7. output.append("\n");
  8. }
  9. return output.toString();
  10. }

请注意,我已经根据您提供的内容为您进行了翻译。如果您有任何其他需要翻译的内容或问题,请随时提问。

英文:

I'm trying to get the method that would return the following String for an integer parameter of 2:

Output:

如何在Java中使用嵌套循环打印数学乘法表,以2×2和3×3矩阵形式返回字符串值

Same for parameter of 3

如何在Java中使用嵌套循环打印数学乘法表,以2×2和3×3矩阵形式返回字符串值

The code which I got so far is below:

  1. public String simpleMultiplicationTable(int num) {
  2. for(int i = 1 ;i&lt;=num;i++) {
  3. for(int j=1;j&lt;=num;j++) {
  4. System.out.format(&quot;%4d&quot;,i*j);
  5. }
  6. System.out.println();
  7. }
  8. return String.valueOf(num);
  9. }

答案1

得分: 0

代码部分已翻译如下:

  1. // 使用 StringBuilder 而不是打印每个值,将其附加到 StringBuilder,并在最后返回。
  2. public class Main {
  3. public static void main(String[] args) {
  4. // 测试
  5. System.out.println(simpleMultiplicationTable(2));
  6. System.out.println(simpleMultiplicationTable(3));
  7. }
  8. public static String simpleMultiplicationTable(int num) {
  9. StringBuilder sb = new StringBuilder();
  10. for (int i = 1; i <= num; i++) {
  11. for (int j = 1; j <= num; j++) {
  12. sb.append(i * j);
  13. if (j < num) {
  14. sb.append(" ");
  15. }
  16. }
  17. if (i < num) {
  18. sb.append("\n");
  19. }
  20. }
  21. return sb.toString();
  22. }
  23. }
  24. **输出:**
  25. 1 2
  26. 2 4
  27. 1 2 3
  28. 2 4 6
  29. 3 6 9
  30. 使用 `String` 替代 `StringBuilder`
  31. ====
  32. public class Main {
  33. public static void main(String[] args) {
  34. // 测试
  35. System.out.println(simpleMultiplicationTable(2));
  36. System.out.println(simpleMultiplicationTable(3));
  37. }
  38. public static String simpleMultiplicationTable(int num) {
  39. String table = "";
  40. for (int i = 1; i <= num; i++) {
  41. for (int j = 1; j <= num; j++) {
  42. table += j < num ? i * j + " " : i * j;
  43. }
  44. if (i < num) {
  45. table = table + "\n";
  46. }
  47. }
  48. return table;
  49. }
  50. }
  51. JUnit 测试
  52. ========
  53. import static org.junit.Assert.assertEquals;
  54. import static org.junit.Assert.fail;
  55. import org.junit.jupiter.api.Test;
  56. class TestMethods {
  57. @Test
  58. public void testSimpleMultiplicationTable() {
  59. Table table = new Table();
  60. String result = table.simpleMultiplicationTable(2);
  61. if (result.contains(" \n")) {
  62. fail("你的输出表格在换行字符之前有一个或多个额外的空格。你可以使用 trim() 函数去除额外的空格");
  63. }
  64. assertEquals("1 2\n2 4", result);
  65. result = table.simpleMultiplicationTable(1);
  66. if (result.contains(" \n")) {
  67. fail("你的输出表格在换行字符之前有一个或多个额外的空格。你可以使用 trim() 函数去除额外的空格");
  68. }
  69. assertEquals("1", result);
  70. }
  71. }
  72. class Table {
  73. public String simpleMultiplicationTable(int num) {
  74. String table = "";
  75. for (int i = 1; i <= num; i++) {
  76. for (int j = 1; j <= num; j++) {
  77. table += j < num ? i * j + " " : i * j;
  78. }
  79. if (i < num) {
  80. table = table + "\n";
  81. }
  82. }
  83. return table;
  84. }
  85. }
英文:

Instead of printing each value, append it to a StringBuilder and return the same at the end.

Do it as follows:

  1. public class Main {
  2. public static void main(String[] args) {
  3. // Tests
  4. System.out.println(simpleMultiplicationTable(2));
  5. System.out.println(simpleMultiplicationTable(3));
  6. }
  7. public static String simpleMultiplicationTable(int num) {
  8. StringBuilder sb = new StringBuilder();
  9. for (int i = 1; i &lt;= num; i++) {
  10. for (int j = 1; j &lt;= num; j++) {
  11. sb.append(i * j);
  12. if (j &lt; num) {
  13. sb.append(&quot; &quot;);
  14. }
  15. }
  16. if (i &lt; num) {
  17. sb.append(&quot;\n&quot;);
  18. }
  19. }
  20. return sb.toString();
  21. }
  22. }

Output:

  1. 1 2
  2. 2 4
  3. 1 2 3
  4. 2 4 6
  5. 3 6 9

Using String instead of StringBuilder:

  1. public class Main {
  2. public static void main(String[] args) {
  3. // Tests
  4. System.out.println(simpleMultiplicationTable(2));
  5. System.out.println(simpleMultiplicationTable(3));
  6. }
  7. public static String simpleMultiplicationTable(int num) {
  8. String table = &quot;&quot;;
  9. for (int i = 1; i &lt;= num; i++) {
  10. for (int j = 1; j &lt;= num; j++) {
  11. table += j &lt; num ? i * j + &quot; &quot; : i * j;
  12. }
  13. if (i &lt; num) {
  14. table = table + &quot;\n&quot;;
  15. }
  16. }
  17. return table;
  18. }
  19. }

JUnit Tests

  1. import static org.junit.Assert.assertEquals;
  2. import static org.junit.Assert.fail;
  3. import org.junit.jupiter.api.Test;
  4. class TestMethods {
  5. @Test
  6. public void testSimpleMultiplicationTable() {
  7. Table table = new Table();
  8. String result = table.simpleMultiplicationTable(2);
  9. if (result.contains(&quot; \n&quot;)) {
  10. fail(&quot;Your output table has one or more extra spaces before the newline character. You can use the trim() function to remove additional spaces&quot;);
  11. }
  12. assertEquals(&quot;1 2\n2 4&quot;, result);
  13. result = table.simpleMultiplicationTable(1);
  14. if (result.contains(&quot; \n&quot;)) {
  15. fail(&quot;Your output table has one or more extra spaces before the newline character. You can use the trim() function to remove additional spaces&quot;);
  16. }
  17. assertEquals(&quot;1&quot;, result);
  18. }
  19. }
  20. class Table {
  21. public String simpleMultiplicationTable(int num) {
  22. String table = &quot;&quot;;
  23. for (int i = 1; i &lt;= num; i++) {
  24. for (int j = 1; j &lt;= num; j++) {
  25. table += j &lt; num ? i * j + &quot; &quot; : i * j;
  26. }
  27. if (i &lt; num) {
  28. table = table + &quot;\n&quot;;
  29. }
  30. }
  31. return table;
  32. }
  33. }

huangapple
  • 本文由 发表于 2020年4月7日 08:14:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/61070876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定