Java第一天---二进制转换

package com.easy;
import java.util.Scanner;
public class Easy {
   //类名首字母必须大写
	public static void main(String[] args) {
   
		//作业:手算45的二进制
		int num;
		Scanner sc = new Scanner(System.in);
		num = sc.nextInt();
		System.out.println(Transform(num));
		sc.close();
	}
	public static String Transform(int a) {
   
		String TempResult = "";
		String result = "0b";
		while(a != 0) {
   
			TempResult += (a % 2 == 0) ? "0" : "1";
			a /= 2;
		}
		for(int i = TempResult.length() - 1; i >= 0; i--) {
   
			result += TempResult.charAt(i);
		}
		result += "\n";
		return result;
	}
}
全部评论

相关推荐

11-03 18:50
门头沟学院 Java
迷茫的大四🐶:问就是马上到,一周五天,6个月以上,全国可飞
点赞 评论 收藏
分享
牛客nb666号:见天才的门槛罢了查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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