题解 | #进制转换#

进制转换

https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String number = in.nextLine();
        String n1 = number.substring(2);
        int output = 0;
        int beishu = 1;
        for (int j = 0; j < n1.length() - 1; j++) {
            beishu = 16 * beishu;
        }
        for (int i = 0; i < n1.length() - 1; i++) {
            char digit = n1.charAt(i);
            int dig = 0;
            if (digit == 'A') {
                dig = 10;
            } else if (digit == 'B') {
                dig = 11;
            } else if (digit == 'C') {
                dig = 12;
            } else if (digit == 'D') {
                dig = 13;
            } else if (digit == 'E') {
                dig = 14;
            } else if (digit == 'F') {
                dig = 15;
            } else {
                dig = Integer.parseInt(String.valueOf(digit));
            }
            output += dig * beishu;
            beishu = beishu / 16;
        }
        char digit1 = n1.charAt(n1.length() - 1);
        int dig = 0;
        if (digit1 == 'A') {
            dig = 10;
        } else if (digit1 == 'B') {
            dig = 11;
        } else if (digit1 == 'C') {
            dig = 12;
        } else if (digit1 == 'D') {
            dig = 13;
        } else if (digit1 == 'E') {
            dig = 14;
        } else if (digit1 == 'F') {
            dig = 15;
        } else {
            dig = Integer.parseInt(String.valueOf(digit1));;
        }
        output += dig;
        System.out.println(output);
    }
}

全部评论

相关推荐

不多说了,看图吧
MomonKa:实际上是,机房机器有些高度,问问你身高,有没有女朋友是看你能不能猛猛加班
你最讨厌面试问你什么?
点赞 评论 收藏
分享
06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务