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

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

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

问题

public String simpleMultiplicationTable(int num) {
    StringBuilder output = new StringBuilder();

    for (int i = 1; i <= num; i++) {
        for (int j = 1; j <= num; j++) {
            output.append(String.format("%4d", i * j));
        }
        output.append("\n");
    }
    return output.toString();
}

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

英文:

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:

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

答案1

得分: 0

代码部分已翻译如下:

// 使用 StringBuilder 而不是打印每个值,将其附加到 StringBuilder,并在最后返回。

public class Main {
    public static void main(String[] args) {
        // 测试
        System.out.println(simpleMultiplicationTable(2));
        System.out.println(simpleMultiplicationTable(3));
    }

    public static String simpleMultiplicationTable(int num) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= num; i++) {
            for (int j = 1; j <= num; j++) {
                sb.append(i * j);
                if (j < num) {
                    sb.append(" ");
                }
            }
            if (i < num) {
                sb.append("\n");
            }
        }
        return sb.toString();
    }
}

**输出:**

1 2
2 4
1 2 3
2 4 6
3 6 9

使用 `String` 替代 `StringBuilder`:
====

public class Main {
    public static void main(String[] args) {
        // 测试
        System.out.println(simpleMultiplicationTable(2));
        System.out.println(simpleMultiplicationTable(3));
    }

    public static String simpleMultiplicationTable(int num) {
        String table = "";
        for (int i = 1; i <= num; i++) {
            for (int j = 1; j <= num; j++) {
                table += j < num ? i * j + " " : i * j;
            }
            if (i < num) {
                table = table + "\n";
            }
        }
        return table;
    }
}

JUnit 测试
======== 

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.jupiter.api.Test;

class TestMethods {

    @Test
    public void testSimpleMultiplicationTable() {
        Table table = new Table();
        String result = table.simpleMultiplicationTable(2);
        if (result.contains(" \n")) {
            fail("你的输出表格在换行字符之前有一个或多个额外的空格。你可以使用 trim() 函数去除额外的空格");
        }
        assertEquals("1 2\n2 4", result);

        result = table.simpleMultiplicationTable(1);
        if (result.contains(" \n")) {
            fail("你的输出表格在换行字符之前有一个或多个额外的空格。你可以使用 trim() 函数去除额外的空格");
        }
        assertEquals("1", result);
    }
}

class Table {

    public String simpleMultiplicationTable(int num) {
        String table = "";
        for (int i = 1; i <= num; i++) {
            for (int j = 1; j <= num; j++) {
                table += j < num ? i * j + " " : i * j;
            }
            if (i < num) {
                table = table + "\n";
            }
        }
        return table;
    }
}
英文:

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

Do it as follows:

public class Main {
public static void main(String[] args) {
// Tests
System.out.println(simpleMultiplicationTable(2));
System.out.println(simpleMultiplicationTable(3));
}
public static String simpleMultiplicationTable(int num) {
StringBuilder sb = new StringBuilder();
for (int i = 1; i &lt;= num; i++) {
for (int j = 1; j &lt;= num; j++) {
sb.append(i * j);
if (j &lt; num) {
sb.append(&quot; &quot;);
}
}
if (i &lt; num) {
sb.append(&quot;\n&quot;);
}
}
return sb.toString();
}
}

Output:

1 2
2 4
1 2 3
2 4 6
3 6 9

Using String instead of StringBuilder:

public class Main {
public static void main(String[] args) {
// Tests
System.out.println(simpleMultiplicationTable(2));
System.out.println(simpleMultiplicationTable(3));
}
public static String simpleMultiplicationTable(int num) {
String table = &quot;&quot;;
for (int i = 1; i &lt;= num; i++) {
for (int j = 1; j &lt;= num; j++) {
table += j &lt; num ? i * j + &quot; &quot; : i * j;
}
if (i &lt; num) {
table = table + &quot;\n&quot;;
}
}
return table;
}
}

JUnit Tests

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.jupiter.api.Test;
class TestMethods {
@Test
public void testSimpleMultiplicationTable() {
Table table = new Table();
String result = table.simpleMultiplicationTable(2);
if (result.contains(&quot; \n&quot;)) {
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;);
}
assertEquals(&quot;1 2\n2 4&quot;, result);
result = table.simpleMultiplicationTable(1);
if (result.contains(&quot; \n&quot;)) {
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;);
}
assertEquals(&quot;1&quot;, result);
}
}
class Table {
public String simpleMultiplicationTable(int num) {
String table = &quot;&quot;;
for (int i = 1; i &lt;= num; i++) {
for (int j = 1; j &lt;= num; j++) {
table += j &lt; num ? i * j + &quot; &quot; : i * j;
}
if (i &lt; num) {
table = table + &quot;\n&quot;;
}
}
return table;
}
}

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:

确定