我正在尝试在java中创建一个程序,该程序将为赋值创建一个10(行)乘15(列)三角形乘法表。在赋值中,我们必须使用常量IMAX = 10和JMAX = 15。我在下面附上了一张桌子应该是什么样子的照片。我已经得到了表的第一个但是我在设法停止数学和第10行以及在列号中添加边框时遇到了麻烦。帮助将不胜感激。
同样为了解决这个问题,我们允许使用if / else语句,for循环和/或while循环
public class Question2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
final int IMAX= 15, JMAX=10;
for (int i = 1; i <= JMAX; i++)
{
System.out.print( i + " ");
}
System.out.println();
System.out.print("--------------------------------------------------------" );
System.out.println();
for (int i = 1; i <= IMAX; i++){
for (int j = 1; j <= i; j++)
{
System.out.print(i*j + " " );
}
System.out.println();
}
}
}
答案 0 :(得分:1)
public static void main(String[] args) {
final int IMAX = 15, JMAX = 10;
System.out.print(" |");
for (int i = 1; i <= JMAX; i++) {
System.out.print(i + " ");
}
System.out.println();
System.out.print("--------------------------------------------------------");
System.out.println();
for (int i = 1; i <= IMAX; i++) {
if (i < 10) {
System.out.print(i + " |");
} else {
System.out.print(i + " |");
}
for (int j = 1; j <= i; j++) {
System.out.print(i * j + " ");
if (j == JMAX) {
break;
}
}
System.out.println();
}
}
答案 1 :(得分:1)
这是Java中的乘法表
import java.util.*;
public class table
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int a = sc.nextInt();
for (int i=1; i <=12; i++)
System.out.println(a+ " x " +i + " = " +a*i);
}
}
您可以根据需要更改变量名称
答案 2 :(得分:0)
这应该做:
public class HelloWorld {
public static void main(String[] args) {
final int IMAX= 15, JMAX=10;
System.out.print(" |");
for (int i = 1; i <= JMAX; i++) {
System.out.printf("%-4d", i);
}
System.out.println();
System.out.print("--------------------------------------------------------" );
System.out.println();
for (int i = 1; i <= IMAX; i++) {
System.out.printf("%-3d|", i);
for (int j = 1; j <= i && j <=JMAX; j++) {
System.out.printf("%-4d", i*j);
}
System.out.println();
}
}
}
答案 3 :(得分:0)
你可以用数百万种不同的方式做到这一点!
public class Test
{
private static final int ROWS = 15;
private static final int COLS = 10;
public static final void main(final String... args) {
for (int i = 0; i <= ROWS; i++) {
if (i < 1) {
System.out.printf("%5s", " | ");
for (int k = 1; k <= COLS; k++) {
System.out.printf("%-4d", k);
}
System.out.println();
System.out.println("____________________________________________");
continue;
}
System.out.printf("%5s", i + " | ");
for (int j = 1; j <= i && j <= COLS; j++) {
System.out.printf("%-4d", i * j);
}
System.out.println();
}
}
}