Java 程序将两个浮点数相乘

javaobject oriented programmingprogramming更新于 2024/4/12 2:52:17

在本文中,我们将了解如何将两个浮点数相乘。浮点数是具有十进制值的数字。浮点数据类型是单精度 32 位 IEEE 754 浮点数。它主要用于在大型浮点数数组中节省内存。默认值为 0.0f。浮点数据类型从不用于精确值(例如货币)。

下面是相同的演示 −

输入

假设我们的输入是 −

Value_1: 12.4f
Value_2: 15.7f

输出

所需的输出将是 −

结果:194.68f

算法

步骤 1- 开始
步骤 2- 声明三个浮点数:input_1、input_2 和 product
步骤 3- 提示用户输入两个浮点值/定义浮点值
步骤 4- 读取值
步骤 5- 使用乘法运算符 (*)
步骤 6- 显示结果
步骤 7- 停止

示例 1

import java.util.Scanner;
public class MultiplyFloatValues{
   public static void main(String[] args){
      float input_1, input_2, my_prod;
      Scanner my_scan = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.println("Enter the first floating point number: ");
      input_1 = my_scan.nextFloat();
      System.out.println("Enter the second floating point number: ");
      input_2 = my_scan.nextFloat();
      my_prod = input_1 * input_2;
      System.out.println("\nThe product of the two values is: " + my_prod);
   }
}

输出

A reader object has been defined
Enter the first floating point number: 12.4
Enter the second floating point number: 15.7
The product of the two values is: 194.68

示例 2

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

public class MultiplyFloatValues{
   public static void main(String[] args){
      float value_1, value_2, my_prod;
      value_1 = 12.4f;
      value_2 = 15.7f;
      System.out.printf("The two numbers are %.2f and %.2f ",value_1, value_2 );
      my_prod = value_1 * value_2;
      System.out.println("\nThe product of the two values are: " + my_prod);
   }
}

输出

The two numbers are 12.40 and 15.70
The product of the two values are: 194.68

相关文章


有用资源