Java Integer floatValue 方法

JavaJavaBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,你将学习 Java Integer 类的 floatValue() 方法,该方法用于将 Integer 对象转换为其对应的浮点数(float)形式。你还将了解该方法的语法、参数和返回值。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/FileandIOManagementGroup(["`File and I/O Management`"]) java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/BasicSyntaxGroup -.-> java/type_casting("`Type Casting`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/FileandIOManagementGroup -.-> java/files("`Files`") java/FileandIOManagementGroup -.-> java/create_write_files("`Create/Write Files`") subgraph Lab Skills java/variables -.-> lab-117710{{"`Java Integer floatValue 方法`"}} java/output -.-> lab-117710{{"`Java Integer floatValue 方法`"}} java/type_casting -.-> lab-117710{{"`Java Integer floatValue 方法`"}} java/user_input -.-> lab-117710{{"`Java Integer floatValue 方法`"}} java/wrapper_classes -.-> lab-117710{{"`Java Integer floatValue 方法`"}} java/files -.-> lab-117710{{"`Java Integer floatValue 方法`"}} java/create_write_files -.-> lab-117710{{"`Java Integer floatValue 方法`"}} end

创建一个 Java 文件

~/project/ 目录下使用以下命令创建一个名为 IntegerFloatValueLab.java 的 Java 文件:

touch ~/project/IntegerFloatValueLab.java

编写代码将 Integer 转换为 float

IntegerFloatValueLab.java 文件中,编写以下 Java 代码,使用 floatValue() 方法将 Integer 对象转换为其对应的浮点数形式:

import java.util.Scanner;

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

         // Take integer input from user
         System.out.print("Enter an integer value: ");
         int num = sc.nextInt();

         // Convert Integer to float
         Integer integerObj = num;
         float floatNum = integerObj.floatValue();

         // Print the float value
         System.out.println("Float value of " + num + " is: " + floatNum);

         sc.close();
      }
}

在这段代码中,我们使用 Scanner 类从用户那里获取一个整数值作为输入。然后通过自动装箱(autoboxing)将输入值转换为 Integer 对象。最后,我们使用 floatValue() 方法将 Integer 对象转换为其对应的浮点数形式,并打印结果。

编译并运行 Java 代码

在终端中使用以下命令编译并运行 Java 代码:

javac ~/project/IntegerFloatValueLab.java
java IntegerFloatValueLab

这将编译并运行 Java 代码。你应该会在终端中看到以下输出:

Enter an integer value: 25
Float value of 25 is: 25.0

修改 Java 代码

修改 Java 代码,在 main() 方法的末尾添加以下代码,将浮点数值转换回 Integer 对象并打印结果:

// Convert float to Integer
Integer intValue = (int) floatNum;

// Print the Integer value
System.out.println("Integer value of " + floatNum + " is: " + intValue);

完整的代码应如下所示:

import java.util.Scanner;

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

         // Take integer input from user
         System.out.print("Enter an integer value: ");
         int num = sc.nextInt();

         // Convert Integer to float
         Integer integerObj = num;
         float floatNum = integerObj.floatValue();

         // Print the float value
         System.out.println("Float value of " + num + " is: " + floatNum);

         // Convert float to Integer
         Integer intValue = (int) floatNum;

         // Print the Integer value
         System.out.println("Integer value of " + floatNum + " is: " + intValue);

         sc.close();
      }
}

编译并运行修改后的 Java 代码

在终端中使用与之前相同的命令编译并运行修改后的 Java 代码:

javac ~/project/IntegerFloatValueLab.java
java IntegerFloatValueLab

这将编译并运行修改后的 Java 代码。你应该会在终端中看到以下输出:

Enter an integer value: 100
Float value of 100 is: 100.0
Integer value of 100.0 is: 100

总结

恭喜你,你已经完成了 Java Integer floatValue() 方法实验!在这个实验中,你学习了 Integer 类的 floatValue() 方法,以及如何使用它将 Integer 对象转换为其对应的浮点数形式。你还学习了如何在终端中编写、编译和运行 Java 代码,以及如何使用不同的输入值测试你的代码。

您可能感兴趣的其他 Java 教程