java-10x15乘法表(嵌套循环)
我需要使用for循环、if-else语句和while循环构造一个java-10x15乘法表(嵌套循环),java,for-loop,while-loop,nested-loops,Java,For Loop,While Loop,Nested Loops,我需要使用for循环、if-else语句和while循环构造一个10x15乘法。下面是它应该是什么样子: 我的代码: import java.util.Scanner; public class Question2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); final int IMAX = 15;
10x15
乘法。下面是它应该是什么样子:
我的代码:
import java.util.Scanner;
public class Question2 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
final int IMAX = 15;
final int JMAX = 10;
System.out.print(" |");
for(int j = 1; j<=JMAX; j++) {
System.out.print(" " + j);
// This printed the top header
}
System.out.println();
System.out.println("____________________________________________________________________________");
for(int i = 1; i<=IMAX; i++) {
System.out.print(" " + i + " |");
for (int j = 1; j <=JMAX; j++) {
if(j <= i)
System.out.print( i*j + " ");
else
System.out.println();
}
}
}
}
}
import java.util.Scanner;
公开课问题2{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
最终int IMAX=15;
最终int JMAX=10;
系统输出打印(“|”);
对于(int j=1;j首先,如果您不知道如何使用调试器,那么现在是学习调试器的好时机。它将帮助您解决未来的语法和逻辑问题
其次,您需要将对println()
的调用移动到外部for循环,您只应在j
等于i
后下移一行。例如:
for(int i = 1; i <= IMAX; i++) {
System.out.print(" " + i + " |");
// 'j' should never exceed 'i', just make that the for loop condition,
// no need for an if statement in the body of the for loop.
for (int j = 1; j <= i; j++) {
System.out.print( i * j + " ");
}
// move call to 'println()' here
System.out.println();
}
您应该将System.out.println();
放在循环之后,而不是else语句中。因为它仍在第二个循环中迭代并打印这些空格以下是我的更新代码:
import java.util.Scanner;
公开课问题2
{
publicstaticvoidmain(字符串[]args)
{
扫描仪键盘=新扫描仪(System.in);
最终int IMAX=15;
最终int JMAX=10;
系统输出打印(“|”);
for(int j=1;jSo)您需要将一个for循环转换为while循环。请尝试搜索“convert for to while循环”非常感谢!!帮了我很多忙..有一件事,桌子需要是10x15..那么你知道当j=10时如何水平停止桌子..再次感谢!基本上我正在尝试添加一个j@HousseinKharroubi将while条件更改为while(j
for(int i = 1; i <= IMAX; i++) {
System.out.print(" " + i + " |");
int j = 1;
do {
System.out.print( i * j + " ");
j += 1;
} while (j <= i);
// move call to 'println()' here
System.out.println();
}
public static void main(String[] args) {
//Scanner keyboard = new Scanner(System.in);
final int IMAX = 15;
final int JMAX = 10;
System.out.print(" |");
for(int i = 1; i <= IMAX; i++) {
System.out.printf("%5d", i);
// This printed the top header
}
System.out.println("\n____________________________________________________________________________");
for(int i = 1; i<=IMAX; i++) {
System.out.printf("%2d | ", i);
int j = 1;
while(j <= i) {
System.out.printf("%-5d", i * j);
j += 1;
}
System.out.println();
}
}
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
____________________________________________________________________________
1 | 1
2 | 2 4
3 | 3 6 9
4 | 4 8 12 16
5 | 5 10 15 20 25
6 | 6 12 18 24 30 36
7 | 7 14 21 28 35 42 49
8 | 8 16 24 32 40 48 56 64
9 | 9 18 27 36 45 54 63 72 81
10 | 10 20 30 40 50 60 70 80 90 100
11 | 11 22 33 44 55 66 77 88 99 110 121
12 | 12 24 36 48 60 72 84 96 108 120 132 144
13 | 13 26 39 52 65 78 91 104 117 130 143 156 169
14 | 14 28 42 56 70 84 98 112 126 140 154 168 182 196
15 | 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225
import java.util.Scanner;
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
final int IMAX = 15;
final int JMAX = 10;
System.out.print(" |");
for(int j = 1; j<=JMAX; j++)
{
System.out.print(" " + j + " ");
// This printed the top header
}
System.out.println();
System.out.println("__________________________________________________________________________________________________________");
for(int i = 1; i <= IMAX; i++)
{
if(i<=9)
System.out.print(" " + i + " |");
else
System.out.print(" " + i + " |");
int j = 1;
while (j <= i)
{
System.out.printf(" " + "%-6d", i * j);
j += 1;
}
// move call to 'println()' here
System.out.println();
}
}