Java 程序生成乘法表

javaobject oriented programmingprogramming更新于 2024/4/13 21:00:17

在本文中,我们将了解如何打印乘法表。乘法表是通过使用 for 循环迭代所需输入 10 次并在每次迭代中将输入值乘以 1 到 10 之间的数字来创建的。

下面是相同的演示 −

输入

假设我们的输入是 −

输入:16

输出

所需的输出将是 −

16 的乘法表是:
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

算法

步骤 1 - 开始
步骤 2 - 声明两个整数值,即 my_input 和 i。
步骤 3 - 从用户读取所需值/定义值
步骤 4 - 使用 for 循环从 1 迭代到 10,并在每次迭代中将数字 1 到
10 与输入相乘。
步骤 5- 每次迭代后显示结果值。
步骤 6- 停止

示例 1

import java.util.Scanner;
public class MultiplicationTable {
   public static void main(String[] args) {
      int my_input, i;
      my_input = 16;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter a number : ");
      my_input = my_scanner.nextInt();
      System.out.println("The multiplication table of " +my_input + " is :");
      for(i = 1; i <= 10; ++i){
         System.out.printf("%d * %d = %d \n", my_input, i, my_input * i);
      }
   }
}

输出

Required packages have been imported
A reader object has been defined
Enter a number : 16
The multiplication table of 16 is :
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

示例 2

这里,整数已经被预先定义,并且它的值被访问并显示在控制台上。

public class MultiplicationTable {
   public static void main(String[] args) {
      int my_input, i;
      my_input = 16;
      System.out.println("The number is defined as " +my_input);
      System.out.println("The multiplication table of " +my_input + " is :");
      for(i = 1; i <= 10; ++i){
         System.out.printf("%d * %d = %d \n", my_input, i, my_input * i);
      }
   }
}

输出

The number is defined as 16
The multiplication table of 16 is :
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

相关文章


有用资源