如何用 Java 编写乘法表?

我正在学习 Java,我得到的任务之一是编写如下所示的乘法表:


 1  2  3  4  5  6  7  8  9 10 // 1

 2  4  6  8 10 12 14 16 18 20 // 2

 3  6  9 12 15 18 21 24 27 30 // 3

....

我已经研究了两天了,但我无法找到答案。我主要关心的是如何编写一个代码来执行乘法到 10 并返回到下一行的下一行。


我已经测试了很多方法,例如下面的代码,但存在问题,我不知道问题出在哪里。


请帮我。


int t = 1;

while(t <= 10) {

    int r = 1;

    int a = 1;

    int b = 1;

    System.out.print(r + " ");

    a = a + 1;

    t++;

}


繁花如伊
浏览 115回答 4
4回答

BIG阳

像这样的表可能最好使用 for 循环来完成:for (int i = 1; i <= 10; i++) {&nbsp; &nbsp; for (int j = 1; j <= 10; j++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(i*j + " ");&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();}如果你还没有学习for循环并且想使用while循环,你可以使用int i = 1;int j = 1;while (i <= 10) {&nbsp; &nbsp; while (j <= 10) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(i*j + " ");&nbsp; &nbsp; &nbsp; &nbsp; j = j + 1;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();&nbsp; &nbsp; i = i + 1;}

慕标琳琳

&nbsp;public class HelloWorld{&nbsp; &nbsp; &nbsp;public static void main(String []args){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Hello World");&nbsp; &nbsp; &nbsp; &nbsp; int count = 1;do {&nbsp; for( int j = 1; j <= 10; j ++) {System.out.print( count*j +""+'\t');}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print('\n');}while (count<11);&nbsp; &nbsp; &nbsp;}}

摇曳的蔷薇

使用内部循环来循环内部public&nbsp; void multiacation(){for(i = 1; i <= 10; i ++) {&nbsp; for(j = 1; i <= 10; i ++) {System.out.println(i*j +"");}}试试这个,然后告诉我它是否有效

忽然笑

尝试两个 for 循环。for(i = 1; i <= 10; i ++) {&nbsp; for(j = 1; i <= 10; i ++) {&nbsp; &nbsp; &nbsp;System.out.print(i*j + " ");&nbsp; }&nbsp; System.out.println();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java