簡體   English   中英

如何使用 Java 中的嵌套循環在 2x2 和 3x3 矩陣中打印返回字符串值的數學乘法表

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

我正在嘗試獲取將為 2 的 integer 參數返回以下字符串的方法:

Output:

在此處輸入圖像描述

參數 3 相同

在此處輸入圖像描述

到目前為止我得到的代碼如下:

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

append 不是打印每個值,而是將其發送到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) {
        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();
    }
}

Output:

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

使用String而不是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 = "";
        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("Your output table has one or more extra spaces before the newline character. You can use the trim() function to remove additional spaces");
        }
        assertEquals("1 2\n2 4", result);

        result = table.simpleMultiplicationTable(1);
        if (result.contains(" \n")) {
            fail("Your output table has one or more extra spaces before the newline character. You can use the trim() function to remove additional spaces");
        }
        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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM