具有for循环的MULTIPLICATION TABLE

时间:2017-12-02 15:47:12

标签: java loops for-loop

我需要制作for循环,它将显示乘法表,数字1,2,3,数字从1到10。

enter image description here

它应该是这样的。提前谢谢!

3 个答案:

答案 0 :(得分:0)

您可以创建for循环,从1到10迭代,然后将它们乘以1,2,3,然后显示它们 您可以创建3个最终变量来存储1,2,3 可能性真的很多;)

for(int i=0;i<10;i++){
System.out.println(i*1);
System.out.println(i*2);
System.out.println(i*3);
}

答案 1 :(得分:0)

<!DOCTYPE HTML>

<html>
<head>
<title>Untitled</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
     $(function(){
        $( ".button" ).click(function() {
            var n = document.getElementById( "num" ).value;
                var arr = [];
                for (i=0; i<n; i++)
                arr[i] = [];
                var sub = 1;
                var count = 1;
                i = 0;
                while (count <= n){
                var loopcount = 1;
                while (loopcount <= n){
                    arr[i].push(sub);
                    sub += count;
                    loopcount++;
                }
                count++;
                sub = count;
                i++;
                }

             var $table = $("<table></table>");
              var $row;
              arr.forEach(function(subArr){ // using forEach will execute the parameter function for every element (subarray)
                $row = $("<tr></tr>"); // create a new row
                subArr.forEach(function(el){ // now another forEach function, this time adding cells to the row
                  $row.append('<td>' + el + '</td>');
                });
                $table.append($row);
              });
              $table.appendTo(document.body);

            });
     });
</script>
</head>
<body>
<div class="wrapper">
    <h1>Generate multiplication tables</h1>
    <form>
        <label for="num">Choose number of rows</label>
        <input type="text" value="" id="num"/>
        <input type="button" value="Submit" class="button"/>
        <p id="answer"></p>
        <div id="myDiv"></div>
    </form>

</div>
</body>
</html>

答案 2 :(得分:0)

您可以尝试以下代码。它会以你想要的方式显示表格:

public class MyClass {
    public static void main(String args[]) {
        int x= 1;
        int y= 2;
        int z= 3;
        int temp;
        for(temp=1;temp<=10;temp++)
            System.out.println(temp + "\t" + x*temp + "\t" + y*temp +"\t" + z*temp);
    }
}

输出:

1   1   2   3
2   2   4   6 
3   3   6   9
4   4   8   12 
5   5   10  15
6   6   12  18
7   7   14  21
8   8   16  24
9   9   18  27
10  10  20  30