介绍
在本实验中,我们将学习 Java 中 Integer 类的 toString() 方法。我们将了解如何使用此方法获取 Integer 对象的字符串表示形式。我们还将通过一些示例来理解其用法。
在本实验中,我们将学习 Java 中 Integer 类的 toString() 方法。我们将了解如何使用此方法获取 Integer 对象的字符串表示形式。我们还将通过一些示例来理解其用法。
创建一个名为 IntegerToString.java 的新 Java 文件。
public class IntegerToString {
public static void main(String[] args) {
// code to be added
}
}
在这一步中,我们将创建一个整数对象,然后使用 toString() 方法将该整数对象转换为字符串。
public class IntegerToString {
public static void main(String[] args) {
// create an Integer object
Integer num = 35;
// convert integer to string
String str = num.toString();
// print the string
System.out.println("String representation of the Integer: " + str);
}
}
输出:
String representation of the Integer: 35
toString() 方法同样适用于负整数。在这一步中,我们将把一个负整数转换为其字符串表示形式。
public class IntegerToString {
public static void main(String[] args) {
// create a negative Integer object
Integer num = -18;
// convert negative integer to string
String str = num.toString();
// print the string
System.out.println("String representation of the Integer: " + str);
}
}
输出:
String representation of the Integer: -18
在这一步中,我们将使用 Scanner 创建一个输入整数,然后使用 toString() 方法获取其字符串表示形式。
import java.util.Scanner;
public class IntegerToString {
public static void main(String[] args) {
// create scanner object
Scanner sc = new Scanner(System.in);
// take input integer from user
System.out.print("Enter an integer: ");
int num = sc.nextInt();
// convert integer to string
String str = Integer.toString(num);
// print the string
System.out.println("String representation of the entered integer: " + str);
}
}
输出:
Enter an integer: 56
String representation of the entered integer: 56
在步骤 4 中,我们没有处理输入不是整数的情况。在这一步中,我们将处理输入不是有效整数时的异常。
import java.util.Scanner;
public class IntegerToString {
public static void main(String[] args) {
// create scanner object
Scanner sc = new Scanner(System.in);
try {
// take input integer from user
System.out.print("Enter an integer: ");
int num = sc.nextInt();
// convert integer to string
String str = Integer.toString(num);
// print the string
System.out.println("String representation of the entered integer: " + str);
} catch(Exception e) {
System.out.println("Invalid input");
}
}
}
输出:
Enter an integer: 34
String representation of the entered integer: 34
在 Java 中,我们可以使用 Integer 类的 toBinaryString() 方法将整数转换为其二进制字符串表示形式。
public class IntegerToString {
public static void main(String[] args) {
// create an integer
int num = 10;
// convert integer to binary string
String binaryString = Integer.toBinaryString(num);
// print binary string
System.out.println("Binary string representation of the Integer: " + binaryString);
}
}
输出:
Binary string representation of the Integer: 1010
在 Java 中,我们可以使用 Integer 类的 toHexString() 方法将整数转换为其十六进制字符串表示形式。
public class IntegerToString {
public static void main(String[] args) {
// create an integer
int num = 255;
// convert integer to hex string
String hexString = Integer.toHexString(num);
// print hex string
System.out.println("Hexadecimal string representation of the Integer: " + hexString);
}
}
输出:
Hexadecimal string representation of the Integer: ff
在本实验中,我们学习了如何使用 Integer 类的 toString() 方法将整数转换为字符串表示形式。我们还学习了如何处理无效输入,并将整数转换为二进制或十六进制字符串表示形式。当我们需要将整数转换为字符串以用于不同场景时,这种方法非常有用。