题目描述
写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
String str;
// //利用decode函数
// while(sc.hasNextLine())
// {
// str = sc.nextLine();//读取字符串
// System.out.println(Integer.decode(str));//decode解码
// }
//for循环解决
while(sc.hasNextLine())
{
str = sc.nextLine();
int len = str.length();
int sum = 0;
for(int i = len -1; i>1; i--)
{
char temp = str.charAt(i);
int num = (int)temp;//char转为int为ASCII
if(num>=65)//ABCDEF,其中A对应的ASCII为65
{
num = num - 65 + 10;
}
else//阿拉伯数字,其中0对应为48
{
num = temp - 48;
}
sum = sum + (int)Math.pow(16,len-i-1)*num;
}
System.out.println(sum);
}
}
}