forked from Doragd/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
字符串字符串字符串
Description
- 写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。
- 输入一个十六进制的数值字符串。
- Interger.parse(" " , 16);
- 一定要注意 Ox OX 开头
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//16进制转10
String inputStr = in.nextLine();
// 移除可能存在的 "0x" 或 "0X" 前缀
if (inputStr.startsWith("0x") || inputStr.startsWith("0X")) {
inputStr = inputStr.substring(2);
}
try {
int outInt = Integer.parseInt(inputStr, 16);
System.out.println(outInt);
} catch (NumberFormatException e) {
System.out.println("输入格式错误: " + e.getMessage());
}
}Metadata
Metadata
Assignees
Labels
字符串字符串字符串