题解 | 进制转换

进制转换

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str;
        int ret=0;

        str = sc.nextLine();
        str = str.substring(2);
        while(str.length()>0){
            int num;
            char c = str.charAt(0);
            if ((c<='9')&&(c>='0')){
                num = c-'0';
            }
            else{
                num = c-'A'+10;
            }
            ret += num*Math.pow(16,str.length()-1);
            str = str.substring(1);
        }
        System.out.println(ret);
    }
}

(c <= '9') && (c >= '0')(注意是单引号 ' 而不是双引号 ",因为 c 是 char 类型)

字符串比较要用单引号 双引号是str

全部评论

相关推荐

12-11 14:24
门头沟学院 Java
牛客35720396...:不要用boss,全是骗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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