Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建乘法表的最佳方法-java_Java_Arrays - Fatal编程技术网

创建乘法表的最佳方法-java

创建乘法表的最佳方法-java,java,arrays,Java,Arrays,您好,我正在尝试创建一个输出乘法网格的java程序,我想知道如果我有n个值,是否有方法在不使用大量if语句的情况下实现它。这是密码 public class MultiplicationGrid { public static void main(String[] args) { int num[][] = new int[4][4]; //String size[][] = new String[1][13]; for(int i = 0; i < num.

您好,我正在尝试创建一个输出乘法网格的java程序,我想知道如果我有n个值,是否有方法在不使用大量if语句的情况下实现它。这是密码

public class MultiplicationGrid {

public static void main(String[] args) {

    int num[][] = new int[4][4];

    //String size[][] = new String[1][13];
    for(int i = 0; i < num.length; ++i) {
        for(int j = 0; j < num[i].length;++j) {
            num[i][j] = (j+1)*(i+1);
        }   
    }

    int count = 0;
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;
    for (int i = 0; i < num.length; ++i)    {
        for(int j = 0; j < num[i].length; ++j) {
        if(count == 0) {
            count = num [i][j];
            continue;
        }
        if(count1 == 0) {
            count1 = num [i][j];
            continue;
        }
        if(count2 == 0) {
            count2 = num [i][j];
            continue;
        }
        if(count3 == 0) {
            count3 = num [i][j];

        }
        System.out.println(count + "    " + (count1) + "    " + (count2) + "    " + (count3));
        count = 0;
        count1 = 0;
        count2 = 0;
        count3 = 0;
        }

    }

}
公共类乘法网格{
公共静态void main(字符串[]args){
int num[][]=新的int[4][4];
//字符串大小[][]=新字符串[1][13];
对于(int i=0;i
}


提前感谢。

您可以定义表格大小并按如下方式打印乘法网格:

 public static void main(String[]args) {
        final int TABLE_SIZE = 12;
        // Declare the rectangular array to store the multiplication table:
        int[][] table = new int[TABLE_SIZE][TABLE_SIZE];

        // Fill in the array with the multiplication table:
        for(int i = 0 ; i < table.length ; ++i) {
          for(int j = 0 ; j < table[i].length ; ++j) {
            table[i][j] = (i+1)*(j+1);
          }
        }

        // Output the table heading
        System.out.print("      :");             // Row name column heading
        for(int j = 1 ; j <= table[0].length ; ++j) {
          System.out.print((j<10 ? "   ": "  ") + j);
        }
        System.out.println("\n-------------------------------------------------------");

        // Output the table contents          
        for(int i = 0 ; i < table.length ; ++i) {
          System.out.print("Row" + (i<9 ? "  ":" ") + (i+1) + ":");

          for(int j = 0; j < table[i].length; ++j) {
            System.out.print((table[i][j] < 10 ? "   " : table[i][j] < 100 ? "  " : " ") + table[i][j]);
          }
          System.out.println();
        }
      }
publicstaticvoidmain(字符串[]args){
最终int表_尺寸=12;
//声明存储乘法表的矩形数组:
int[][]表格=新的int[表格大小][表格大小];
//用乘法表填充数组:
对于(int i=0;i对于(int j=1;j),在编程中很少有一种解决方案明显优于其他解决方案。因此,寻求“最佳方法”一般来说,这不是一个好问题。但是你的方法似乎不是最优的。为什么你需要4个额外的变量?你是对的。我将编辑这个问题。4个变量用于存储数组中的每个值,因为我希望输出是水平的。非常感谢Vidhika。