当前位置:网站首页>每日练习------输出一个整数的二进制数、八进制数、十六进制数。
每日练习------输出一个整数的二进制数、八进制数、十六进制数。
2022-07-30 05:34:00 【北柠陌语】
题目:输出一个整数的二进制数、八进制数、十六进制数。
解题关键:清楚每个进制之间的转换;转换完一个进制之后需要重新设置一遍初始值
思路:1)输出一个整数
2)重新设置num10,不然在每次转换完一次进制后,num10都会变成0
3)二进制转换
4)八进制
5)十六进制
过程: 接下来我们根据我们的解题思路来一步步写代码
第一种方法:
1)输出一个整数
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数");
int num10 = sc.nextInt();
2)重新设置num10,不然在每次转换完一次进制后,num10都会变成0
int a = num10;
int b = num10;
int c = num10;
System.out.println("您输入的整数为:" + num10);
3)二进制转换
String er = "";
while (a != 0) {
er = " " + a % 2 + er;//每个位置的数字用空格隔开再连接
a = a / 2;
}
System.out.println("二进制为:" + er);
4)八进制
String ba = "";
while (b != 0) {
ba = " " + b % 8 + ba;//每个位置的数字用空格隔开再连接
b = b / 8;
}
System.out.println("八进制为:" + ba);
5)十六进制
String shiliu = "";
while (c != 0) {
6)判断余数为10,11,12,13,14,15时的情况
if(c%16==10){
shiliu = " " + "A" + shiliu;
}else if(c%16==11){
shiliu = " " + "B" + shiliu;
}else if(c%16==12){
shiliu = " " + "C" + shiliu;
}else if(c%16==13){
shiliu = " " + "D" + shiliu;
}else if(c%16==14){
shiliu = " " + "E" + shiliu;
}else if(c%16==15){
shiliu = " " + "F" + shiliu;
}else{
shiliu = " " + c%16 + shiliu;
}
c = c / 16;
}
System.out.println("十六进制为:" + shiliu);
完整结果如下:

为了方便大家使用,下面附上源码:
//1)输出一个整数
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数");
int num10 = sc.nextInt();
//2)重新设置num10,不然在每次转换完一次进制后,num10都会变成0
int a = num10;
int b = num10;
int c = num10;
System.out.println("您输入的整数为:" + num10);
// 3)二进制转换
String er = "";
while (a != 0) {
er = " " + a % 2 + er;//每个位置的数字用空格隔开再连接
a = a / 2;
}
System.out.println("二进制为:" + er);
// 4)八进制
String ba = "";
while (b != 0) {
ba = " " + b % 8 + ba;//每个位置的数字用空格隔开再连接
b = b / 8;
}
System.out.println("八进制为:" + ba);
// 5)十六进制
String shiliu = "";
while (c != 0) {
//6)判断余数为10,11,12,13,14,15时的情况
if(c%16==10){
shiliu = " " + "A" + shiliu;
}else if(c%16==11){
shiliu = " " + "B" + shiliu;
}else if(c%16==12){
shiliu = " " + "C" + shiliu;
}else if(c%16==13){
shiliu = " " + "D" + shiliu;
}else if(c%16==14){
shiliu = " " + "E" + shiliu;
}else if(c%16==15){
shiliu = " " + "F" + shiliu;
}else{
shiliu = " " + c%16 + shiliu;
}
c = c / 16;
}
System.out.println("十六进制为:" + shiliu);第二种方法:
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = sc.nextInt();
System.out.println("你输入的整数" + num + "转换为二进制为:" + Integer.toBinaryString(num));
System.out.println("你输入的整数" + num + "转换为八进制为:" + Integer.toOctalString(num));
System.out.println("你输入的整数" + num + "转换为十六进制为:" + Integer.toHexString(num));总结:
这道题有俩种解题方法,第二种方法比较简单是Java种自带的程序,直接引用就可以了,第一种方法是不通过系统自动的程序写,虽然比较麻烦,但大家也是需要理解的,这样对培养我们的编程思想有很大帮助.
明日练习:生成13位条形码
Ean-13码规则:第十三位数字是前十二位数字经过计算得到的校验码。
大家可以自己写写,明天中午12点我准时发出我的写法哦,明天12点不见不散
一生朋友一生情,一生有你才会赢;千山万水总是情,点个关注行不行!

边栏推荐
- 从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
- 839. 模拟堆
- idea 编译protobuf 文件的设置使用
- 手把手教你设计一个CSDN系统
- WeChat payment and payment callback
- MySql fuzzy query Daquan
- JVM之GC 调优工具 Arthas 实战使用(二)
- Error: npm ERR code EPERM
- [Image processing] Image skeleton extraction based on central axis transformation with matlab code
- MySQL模糊查询性能优化
猜你喜欢

解决phpstudy无法启动MySQL服务
![[Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code](/img/c1/f962f1c1d9f75732157d49a5d1d0d6.png)
[Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code

丑陋的程序员

PyCharm usage tutorial (more detailed, picture + text)

net start mysql MySQL service is starting. MySQL service failed to start.The service did not report any errors.

程序员赚钱实操,手把手教你做付费课程,自媒体,付费文章及付费技术课赚钱

文档在线化管理系统Confluce使用

留念 · 大学时代最后的系统设计图
![[其他] DS5](/img/20/6863bb7b58d2e60b35469ba32e5830.png)
[其他] DS5

解决没有配置本地nacos但是一直发生localhost8848连接异常的问题
随机推荐
This article will take you through js to deal with the addition, deletion, modification and inspection of tree structure data
PyCharm使用教程(较详细,图+文)
瑞吉外卖项目:新增菜品与菜品分页查询
Docker-compose安装mysql
MySQL-Explain详解
【Koltin Flow(二)】Flow操作符之末端操作符
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
MYSQL-InnoDB的线程模型
MySQL (2)
cnpm安装步骤
【Koltin Flow(一)】五种创建flow的方式
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
最新版MySQL 8.0 的下载与安装(详细教程)
解决没有配置本地nacos但是一直发生localhost8848连接异常的问题
[GStreamer] The name of the plugin should match GST_PLUGIN_DEFINE
MySql fuzzy query Daquan
4461. 范围分区(Google Kickstart2022 Round C Problem B)
leetcode刷题
Participate in open source, let programmers regain their blood and passion
cookie和session区别