简体   繁体   English

java中的三角乘法表

[英]Triangular multiplication table in java

I'm trying to create a program in java that would create a 10 (row) by 15 (column) triangular multiplication table for an assignment.我正在尝试在 Java 中创建一个程序,该程序将为分配创建一个 10(行)乘以 15(列)的三角形乘法表。 In the assignment we have to use constants IMAX= 10 and JMAX=15.在赋值中,我们必须使用常数 IMAX=10 和 JMAX=15。 I have attached a picture below of what the table should look like.我在下面附上了一张桌子应该是什么样子的图片。 I've gotten the jist of the table but am having trouble trying to stop the math and row 10 and also adding a border in the column numbers.我已经得到了表格的 jist,但是在尝试停止数学和第 10 行以及在列号中添加边框时遇到了麻烦。 Help would be appreciated.帮助将不胜感激。
Also to do this problem, we are permitted to use if/else statements, for loop and/or while loop同样为了解决这个问题,我们可以使用 if/else 语句、for 循环和/或 while 循环

Multiplication table link乘法表链接

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();
        }           
    }           
}
   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();
        }
    }

Here is multiplication table in java这是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);
    }
}

You can change the variable names whatever you want您可以随意更改变量名称

This should do:这应该做:

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();
        }

    } 

}

You could do it in millions different ways!你可以用数百万种不同的方式做到这一点!

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();
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM