简体   繁体   English

通过在Java中循环创建乘法表

[英]Creating multiplication table by looping in Java

I was tasked to make a multiplication table from 1-10 but I was not satisfied with my code, it seems to be long: 我的任务是制作一个从1到10的乘法表,但是我对我的代码不满意,这似乎很长:

for (int i = 1; i <= 10; i++)
    {
        System.out.println("1x" + i + " =  " + i + "\t" + "2x" + i + " =  " + (2*i)
                + "\t" + "3x" + i + " =  " + (3*i) + "\t" + "4x" + i + " =  " + (4*i)
                + "\t" + "5x" + i + " =  " + (5*i) + "\t" + "6x" + i + " =  " + (6*i)
                + "\t" + "7x" + i + " =  " + (7*i) + "\t" + "8x" + i + " =  " + (8*i)
                + "\t" + "9x" + i + " =  " + (9*i) + "\t" + "10x" + i + " =  " + (10*i));
    }

Output: 输出:

1x1 = 1   2x1 = 2
1x2 = 2   2x2 = 4
etc.

Try something like 尝试类似

for (int i = 1; i <= 10; i++) {
   for (int j = 1; j <= 10; j++) {
      System.out.println(i + "x" + j + "=" (i*j));
   }
}

so you have an inner and an outer loop, controlling what you want multiplied and what you want it multiplied by . 因此,您有一个内循环和一个外循环,控制要乘以的值和乘以要乘的值

To be more explicit you could (should?) rename i and j as multiplier and multiplicand 更明确地说,您可以(应该?)将ij重命名为multipliermultiplicand

This will format the table how you have it in your example code, and uses two loops: 这将以示例代码的形式格式化表格,并使用两个循环:

for (int i = 1; i <= 10; i++) {
        for (int j = 1; j <= 10; j++) {
            System.out.print(i + "x" + j + "=" + (i * j) + "\t");
        }
        System.out.println();
    }

The outer loop controls the rows in the multiplication table and the inner loop controls the columns in the multiplication table. 外循环控制乘法表中的行,内循环控制乘法表中的列。 System.out.println() signifies moving into a new row of the table System.out.println()表示移至表的新行

You could use two loops: 您可以使用两个循环:

for (int i = 1; i <= 10; i++)
{
    for (int j = i; j <= 10; j++)
    {
        System.out.println(i + "x" + j + "=" + (i*j));
    }
}
for (int i = 1; i <= 10; i++)
{
    for(int j=1; j<10; j++){
        System.out.println(j+"x"+i+"="+(j*i)+"\t");
    }
    System.out.println("\n");
}
import java.util.Scanner;


public class TableMultiplication {


public static void main(String[] args) {
    int table,count,total;
    //Reading user data from console
    Scanner sc = new Scanner(System.in);
    //reading  data for table 
    System.out.println("Enter Your Table:");
    table = sc.nextInt();
    //reading input for how much  want to count
    System.out.println("Enter Your Count to Table:");
    count = sc.nextInt();
     //iterate upto the user required to count and multiplay the table and store in the total and finally display
    for (int i = 1; i < count; i++) {

        total = table*i;
        System.out.println(table+"*"+i+"="+total);

    }//for








}//main
}//TableMultiplication 
import java.util.Scanner;

public class P11 {

public static void main(String[] args) {
    Scanner s1=new Scanner(System.in);
    System.out.println("Enter a value :");
    int n=s1.nextInt();
    for(int i=1;i<=10;i++)
    {
        for(int j=1;j<=10;j++)
        {
             System.out.println(i+"x"+j+ "="+(i*j)+"\t");   
        }
    }
}

}

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

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