题解 | #进制转换#

进制转换

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

Scanner sc = new Scanner(System.in);
    int res = 0; //定义最终值
    while(sc.hasNextLine()){
      String str = sc.nextLine();
      String s1 =str.substring(2); //截取字符串,省的在下标上晕
      System.out.println(s1);
      for(int i=s1.length()-1;i>=0;--i){ //从右到左开始计算
         char c = s1.charAt(i);
         int num = 0;  //记录每一位的十进制值
         if(c>='0' && c<='9')  //切忌一定要 >= <=
            num = c-'0';   //char类型 转换成int技巧
         else if( c>='A' && c<='F'){
           num = c-'A'+10;
           System.out.println(num);
         }
         else if( c>='a' && c<='f')
           num = c-'a'+10;

         res = (int) (res + num* Math.pow(16,s1.length()-i-1));
        System.out.println(res);
      }
      System.out.println(res);
    }

全部评论

相关推荐

05-23 19:02
吉林大学 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 10:39
一个证都没&nbsp;我能填什么
程序员小白条:别人有,你为什么没有,还是这个道理,社会就是比较,竞争,淘汰,你要安逸,那么就要做好淘汰的准备
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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