Java 程序:两个浮点数相乘

编写一个 Java 程序来计算两个浮点数的乘积并显示结果。在此示例中,我们声明了两个浮点数,分别为 first 和 second,其值分别为 2.7f 和 6.8f。接下来,我们使用第三个变量来保存或存储这两个数的乘积。

package NumPrograms;

public class MultiplyTwoFloats1 {

	public static void main(String[] args) {
		
		float first = 2.7f;
		float second = 6.8f;
		
		float third = first * second;
		
		System.out.println("Product of two Floating Point Numbers = " + third);
	}
}
Java Program to Multiply Two Floating Point Numbers

这个 Java 程序允许输入两个浮点数并打印乘积。

package NumPrograms;

import java.util.Scanner;

public class MultiplyTwoFloats2 {
private static Scanner sc;

public static void main(String[] args) {
sc = new Scanner(System.in);

System.out.print("Enter the First = ");
float first = sc.nextFloat();

System.out.print("Enter the Second = ");
float second = sc.nextFloat();

float third = first * second;

System.out.println("Product of two Floating Point Numbers = " + third);
}
}
Enter the First =  11.98
Enter the Second =  13.76
Product of two Floating Point Numbers = 164.8448

在此 示例 中,multiplicationofTwo 函数接受两个浮点数并返回它们的乘积。

package NumPrograms;

import java.util.Scanner;

public class MultiplyTwoFloats3 {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter the First =  ");	
		float first = sc.nextFloat();
		
		System.out.print("Enter the Second =  ");
		float second = sc.nextFloat();
		
		float third = multiplicationofTwo(first, second);
		
		System.out.println("Product of two Floating Point Numbers = " + third);
	}
	
	public static float multiplicationofTwo(float a, float b) {
		return a * b;
	}
}
Enter the First =  22.5
Enter the Second =  33.98
Product of two Floating Point Numbers = 764.55