21xrx.com
2024-03-29 14:00:35 Friday
登录
文章检索 我的文章 写文章
Java中的乘法表
2021-07-08 16:20:31 深夜i     --     --
J a v a

Java 程序,用于打印用户使用 for 循环输入的数字的乘法表。 您可以修改它 for while 或 do while 循环练习。 使用嵌套循环(另一个循环内的循环),我们可以打印给定范围(例如 a 到 b)之间的数字表,例如,如果输入数字是“3”和“6”,则打印“3”、“4”表 ', '5' 和 '6' 将被打印。

 

打印乘法表的Java程序

import java.util.Scanner;


class MultiplicationTable
{
  public static void main(String args[])
  {
    int n, c;
    System.out.println("Enter an integer to print it's multiplication table");
    Scanner in = new Scanner(System.in);
    n = in.nextInt();
    System.out.println("Multiplication table of " + n);

    for (c = 1; c <= 10; c++)
      System.out.println(n + "*" + c + " = " + (n*c));
  }
}

 

程序输出:

下载乘法表程序类文件。

代码:

import java.util.Scanner;


class Tables
{
  public static void main(String args[])
  {
    int a, b, c, d;

    System.out.println("Enter range of numbers to print their multiplication tables");
    Scanner in = new Scanner(System.in);

    a = in.nextInt();
    b = in.nextInt();

    for (c = a; c <= b; c++) {
      System.out.println("Multiplication table of "+c);

      for (d = 1; d <= 10; d++) {
        System.out.println(c+"*"+d+" = "+(c*d));
      }
    }
  }
}

 

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复