这里我们解释如何编写一个 Java 程序来查找两个整数的乘积。所以首先,我们必须使用 public 修饰符声明 ProductOfTwoNumbers 类。下面的代码显示了一个类声明的示例。
ProductOfTwoNumbers
{
}
这里的类名为 ProductOfTwoNumbers,修饰符为 public。
首先,我们必须声明 ProductOfTwoNumbers 类,然后我们打开 main() 函数。然后我们声明 n1、n2、product 作为整型数据类型。然后我们设置 n1=10,n2=2。然后我们执行乘法,product=n1*n2。现在 product 的值变为 20。然后我们可以使用 System.out.println 将两个数字的乘积显示为 product 的值。
第 1 步:使用 public 修饰符声明 ProductOfTwoNumbers 类
第 2 步:打开 main() 函数以开始程序,Java 程序执行从 main() 函数开始。
第 3 步:声明变量 n1、n2、product 为整型。
第 4 步:赋值 n1=10,n2=2。
第 5 步:计算乘积,即 n1*n2。
第 6 步:使用 System.out.println 显示乘积。
public class ProductOfTwoNumbers {
public static void main(String[] args) {
int n1=10,n2=2,product;//Declare the variables.
product=n1*n2;//Calculate the product.
System.out.println("Product of two integer numbers: "+product);//Display the product
}
}
Product of two integer numbers: 20