题目描述
写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )
输入描述:
输入一个十六进制的数值字符串。
输出描述:
输出该数值的十进制字符串。
思路一:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext())
{
String s = scanner.next();
System.out.println(Integer.decode(s));
}
}
}
思路二:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext())
{
String s = scanner.next().substring(2);
System.out.println(Integer.parseInt(s, 16));
}
}
}
思路三:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext())
{
String s = scanner.next().substring(2);
int num = 0;
for (int i = s.length() - 1; i >= 0; i--)
{
if (s.charAt(i) >= 'A' && s.charAt(i) <= 'F')
num += Integer.valueOf(s.charAt(i) - 'A' + 10) * Math.pow(16, s.length() - 1 - i);
else if (s.charAt(i) >= 'a' && s.charAt(i) <= 'f')
num += Integer.valueOf(s.charAt(i) - 'a' + 10) * Math.pow(16, s.length() - 1 - i);
else
num += Integer.valueOf(s.charAt(i) - '0') * Math.pow(16, s.length() - 1 - i);
}
System.out.println(num);
}
}
}