Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops - Fatal编程技术网

java中的简单乘法数组

java中的简单乘法数组,java,loops,Java,Loops,我想创建一个只包含简单循环和数组的简单java程序。应该是乘法表 如果行是3,列是5,那么它应该显示行和列,并且在矩阵中应该给出行和列的乘法。输出应该是这样的 1 2 3 4 5 1 1 2 3 4 5 2 2 4 6 8 10 3 3 6 9 12 15 我想用简单的循环来创建它。我是java新手,所以我不知道如何才能做到这一点。请让我知道 我已经完成了代码,直到这里 导

我想创建一个只包含简单循环和数组的简单java程序。应该是乘法表

如果行是3,列是5,那么它应该显示行和列,并且在矩阵中应该给出行和列的乘法。输出应该是这样的

     1    2    3    4    5
1    1    2    3    4    5
2    2    4    6    8    10
3    3    6    9    12   15
我想用简单的循环来创建它。我是java新手,所以我不知道如何才能做到这一点。请让我知道

我已经完成了代码,直到这里

导入java.util.*

class cross_multiplication
{

    public static void main(String a[])
        {

        System.out.println("How many rows required? : ");
        Scanner in1 = new Scanner(System.in);
        int num_rows = in1.nextInt();

        System.out.println("How many cols required? : ");
        Scanner in2 = new Scanner(System.in);
        int num_cols = in2.nextInt();

    //int arr1 [] = new int[num_rows]; 
    //int arr2 [] = new int[num_cols];      

        for(int i=0;i<num_rows;i++)
        {
                if (i==0)
                {
                    System.out.print("");
                }
                else
                {
                    System.out.print(i);            
                }
                System.out.print("\t");     

        }


    }
}
类交叉乘法
{
公共静态void main(字符串a[]
{
System.out.println(“需要多少行?:”;
扫描器in1=新扫描器(System.in);
int num_rows=in1.nextInt();
System.out.println(“需要多少列:”;
扫描器in2=新扫描器(System.in);
int num_cols=in2.nextInt();
//int arr1[]=新int[num_行];
//int arr2[]=新int[num_cols];

对于(int i=0;i您可以尝试以下方法:

private static void print(final int[][] table){
    for(int r = 0; r < table.length; r++){
        for(int c = 0; c < table[r].length; c++){
            System.out.printf("%d\t", table[r][c]);
        }
        System.out.println();
    }
}

private static int[][] table(final int rows, final int columns){
    final int[][] array = new int[rows][columns];
    for(int r = 1; r <= rows; r++)
        for(int c = 1; c <= columns; c++)
            array[r-1][c-1] = r * c;
    return array;
}
输出将如下所示:

private static void print(final int[][] table){
    for(int r = 0; r < table.length; r++){
        for(int c = 0; c < table[r].length; c++){
            System.out.printf("%d\t", table[r][c]);
        }
        System.out.println();
    }
}

private static int[][] table(final int rows, final int columns){
    final int[][] array = new int[rows][columns];
    for(int r = 1; r <= rows; r++)
        for(int c = 1; c <= columns; c++)
            array[r-1][c-1] = r * c;
    return array;
}

我不会给你答案,但我会给你一些伪代码

您正确设置了2个循环

Loop x = 1 to 3
    Loop y = 1 to 3
        //Do stuff
    End innerloop
End outerloop
这将把你所有的解打印在一条直线上。但是你显然希望它是在一个矩阵中。答案是简单的更改,只需要一行代码。在你的内部循环的每个完整循环之后,你基本上完成了一行乘法(想想为什么)。因此,解决方案是,在内部循环完成运行后,在转到x的下一个外部循环值之前,您需要打印一行新行。总之,我们有如下内容:

Loop x = 1 to 3
    Loop y = 1 to 3
        z = x * y
        Print z + " "
    End innerloop
    Print NewLine // "\n" is the way to do that
End outerloop
试一试

public static void print(int x, int y) {
    for (int i = 1; i <= x; i++) {

        for (int j = 1; j <= y; j++) {

            System.out.print(" " + i * j);
        }
        System.out.println();

    }
}
公共静态无效打印(int x,int y){

要使(int i=1;i包含标题,需要检查您是在第0行(
j==0
)还是第0列(
i==0
)。方法示例:

public static void print(int x, int y) {
  for (int i = 0; i <= x; i++) {
    for (int j = 0; j <= y; j++) {
      if(i==0) { // first row
        if(j>0) {
          System.out.printf("%d\t", j);
        }
        else { // first row, first column: blank space
          System.out.printf("\t");
        }
      }
      else {
        if(j == 0) { // first column
          System.out.printf("%d\t", i); 
        }
        else { // actually in the body of the table - finally!
          System.out.printf("%d\t" i * j);
        }
      }
    }
    System.out.println();
  }
}
公共静态无效打印(int x,int y){

对于(int i=0;我使用数组和循环,然后在尝试后询问您是否尝试过任何代码?不确定为什么您需要数组,但循环的简单复合
应该可以做到这一点。.我已经尝试过。请不要否决。我将粘贴code@PrasadKharkar请检查代码。现在我们只需将标题添加回…@Floris t他将能够自己添加标题。我没有说你必须为他添加标题。因此,“我们必须添加”,而不是“嘿,乔希,你忘了标题”。边做边学…@Floris:对不起,我不明白你说了什么。@sam-在你最初的“期望输出”中您在表上方有一个单独的行来表示一个因素,在表的左侧也有一列。Josh提供的代码不包括这些“标题”。我相信您可以找到如何添加它们(您在问题中截取的代码已经打印出了顶行的标题…现在您只需要一个
if(I==0)
键入语句以打印第一列。有意义吗?