在Java中使用方法的乘法表

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

Multiplication table in Java using a method

问题

public static String multiplicationTable(int rows, int columns) {
    StringBuilder result = new StringBuilder();

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= columns; j++) {
            int num = i * j;
            result.append(num).append("\t");
        }
        result.append("\n");
    }

    return result.toString();
}

public static void testMT() {
    System.out.println("Testing Multiplication Table");

    String expected = "1\t2\t3\t4\t\n2\t4\t6\t8\t\n3\t6\t9\t12\t\n";
    System.out.print("Expecting:\n" + expected);

    String actual = multiplicationTable(3, 4);
    System.out.print("Actual:\n" + actual);

    boolean correct = expected.equals(actual);
    System.out.println("Outputs equal? " + correct);
}
英文:

Write a method that returns a multiplication table based on two input values that specify what two ranges of numbers to multiply together. For example, if the method were given 3 and 4 as input, it would return a string that when printed, would look like this:

1 2 3 4

2 4 6 8

3 6 9 12

Output Requirements:

Each number must be followed by a tab character.
Each line must be followed by a new line character (including the last row).
The columns and rows should range from 1 to the input number.
The method signature should look as follows:

public static String multiplicationTable(int rows, int columns){}

Call the testMT() method from main after completing this method to ensure it is working as expected.

public static String multiplicationTable(int rows, int columns) {

        for(int i = 1; i &lt;= rows; i++){
            for(int j = 1; j &lt;= columns; j++) {
                int num = i * j;
                String a = &quot;&quot; + num +&quot;\t&quot;;
            }
            System.out.println(&quot;&quot;);
        }
        return String.format(&quot;%s&quot;, a); 
    }
public static void testMT() {
     System.out.println(&quot;Testing Multiplication Table&quot;);

     String expected = &quot;1\t2\t3\t4\t\n2\t4\t6\t8\t\n3\t6\t9\t12\t\n&quot;;
     System.out.print(&quot;Expecting:\n&quot; + expected);

     String actual = multiplicationTable(3, 4);
     System.out.print(&quot;Actual:\n&quot; + actual);

     boolean correct = expected.equals(actual);
     System.out.println(&quot;Outputs equal? &quot; + correct);
     }

Here is my output:

Testing Multiplication Table
Expecting:

1 2 3 4
2 4 6 8
3 6 9 12

Actual:

12 Outputs equal? false

I feel like I have the right setup but I can't figure out how to get the expected output.

答案1

得分: 0

您的multiplicationTable方法正在打印而不是返回结果。您可以像这样构建表格并返回它:

public static String multiplicationTable(int rows, int columns) {
    StringBuilder sb = new StringBuilder();
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= columns; j++) {
            int num = i * j;
            sb.append(num).append('\t');
        }
        sb.append('\n');
    }
    return sb.toString();
}

没有其他更改,您将得到以下输出:

Testing Multiplication Table
Expecting:
1	2	3	4	
2	4	6	8	
3	6	9	12	
Actual:
1	2	3	4	
2	4	6	8	
3	6	9	12	
Outputs equal? true
英文:

Your multiplicationTable method is printing, and not quite legal. Build the table and return it. Something like,

public static String multiplicationTable(int rows, int columns) {
	StringBuilder sb = new StringBuilder();
	for (int i = 1; i &lt;= rows; i++) {
		for (int j = 1; j &lt;= columns; j++) {
			int num = i * j;
			sb.append(num).append(&#39;\t&#39;);
		}
		sb.append(&#39;\n&#39;);
	}
	return sb.toString();
}

With no other changes, I then get

Testing Multiplication Table
Expecting:
1	2	3	4	
2	4	6	8	
3	6	9	12	
Actual:
1	2	3	4	
2	4	6	8	
3	6	9	12	
Outputs equal? true

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

发表评论

匿名网友

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

确定