控制台输入一个十进制整数,然后使用原生代码显示对应的二进制数

示例代码:

import java.util.Scanner;
public class Calculate {
/**
* 控制台输入一个十进制整数,然后使用原生代码显示对应的二进制数
*/
public static void main(String[] args) {
String s = "";//定义一个空字符串
Scanner input = new Scanner(System.in);
System.out.println("请输入一个整数");
int convert = input.nextInt();
while (convert > 0) {
//因为十进制转二进制是:除2取余倒计,所以拼接在串前面
s = convert % 2 + s;
convert /= 2;
}
System.out.println(Integer.valueOf(s));
}
}

输出示例 :

posted @ 2021-04-23 15:37  小盆友在学习  阅读(235)  评论(0)    收藏  举报