题解 | 进制转换

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String inputHex = in.nextLine();
        inputHex = inputHex.substring(2);
        int result = 0;
        for( int i = 0 ; i < inputHex.length() ; i++ ){
            int position = inputHex.length() - i - 1;
            int value = Integer.parseInt(alphaBetHandler(inputHex.substring(i,i+1)));

            result = result + value * (int)Math.pow(16,position);
        }
        System.out.println(result);
    }

    public static String alphaBetHandler(String inputCharactor){
        String result = inputCharactor;
        if(inputCharactor.equals("A")){
            result = "10";
        } else if (inputCharactor.equals("B")) {
            result = "11";
        } else if (inputCharactor.equals("C")) {
            result = "12";
        } else if (inputCharactor.equals("D")) {
            result = "13";
        } else if (inputCharactor.equals("E")) {
            result = "14";
        } else if (inputCharactor.equals("F")) {
            result = "15";
        }
        return result;
    }
}

全部评论

相关推荐

2024-12-25 17:57
Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务