挺有成就感的一个小程序,二进制转十进制。

代码如下:

//JHTP Exercise 4.31: Printing the Decimal Equivalent of a Binary Number
//by pandenghuang@163.com
/*(Printing the Decimal Equivalent of a Binary Number) Write an application that inputs an
*integer containing only 0s and 1s (i.e., a binary integer) and prints its decimal equivalent. [Hint: Use
*the remainder and division operators to pick off the binary number’s digits one at a time, from right
*to left. In the decimal number system, the rightmost digit has a positional value of 1 and the next
*digit to the left a positional value of 10, then 100, then 1000, and so on. The decimal number 234
*can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. In the binary number system, the rightmost digit has
*a positional value of 1, the next digit to the left a positional value of 2, then 4, then 8, and so on.
*The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or, 13.]*/import java.util.Scanner;
import java.math.*;public class Binary2Decimal {public static void main(String[] args) {int binary=0;int decimal=0;int len=0;int[] binaryDigit=new int[10];int counter=0;int tempBinary=0;Scanner input=new Scanner(System.in);System.out.print("请输入要转换的二进制数字:");binary=input.nextInt();tempBinary=binary;//1011len=String.valueOf(tempBinary).length();while (counter<len){binaryDigit[counter]=(int)(tempBinary%10);tempBinary/=10;counter++;}counter=0;//decimal=1*1+1*2+0*2*2+1*2*2*2=11while (counter<len){decimal+=binaryDigit[counter]*(int)Math.pow(2, counter);counter++;}System.out.printf("二进制数%d对应的十进制数为: %d",binary,decimal);}
}

运行结果:

请输入要转换的二进制数字:1100001110
二进制数1100001110对应的十进制数为: 782

请输入要转换的二进制数字:10011110
二进制数10011110对应的十进制数为: 158



10位以内二进制数字转十进制计算器(Printing the Decimal Equivalent of a Binary Number)相关推荐

  1. 7-18 Decimal Equivalent of a Binary Number (10 分)

    Input an integer containing only 0s and 1s (i.e., a "binary" integer) and print its decima ...

  2. Java 十进制正小数转为其他进制正小数(小数点后保留10位)

    N进制小数 题目描述: 编写程序实现将任意10进制正小数m转换成n进制的正小数,小数点后保留10位小数 解答要求: 时间限制1000ms,内存限制64ms 输入: 输入包含两个数m.n,用空格隔开.输 ...

  3. LCD1602,4位数据总线液晶屏时钟,STC12C5A60S2的10位ADC功能程序

    /* 程序名: LCD1602,4位数据总线液晶屏时钟,STC12C5A60S2的10位ADC功能程序 编写时间: 2015年10月4日 硬件支持: LCD1602液晶屏 STC12C5A60S2 外 ...

  4. 华为云大数据存储的冗余方式是三副本_揭秘!10+位DBA大神测评华为云DDS增强版实感...

    数据库.操作系统.中间件并称为核心基础软件,在 IT 软件堆栈中起到中流砥柱的作用.今天运行的绝大多数企业应用软件都离不开数据库的支持. 随着移动互联网.物联网.云计算.大数据等新技术爆发式发展,图片 ...

  5. 输入一个4位以内的正整数,输出各位数字之和

    问题描述: 输入一个4位以内的正整数,输出各位数字之和. 解题思路: 首先根据题意,我们先求出这个数的各个数位的数字是多少,然后再让它们相加.求各个数位的数字,最常见的方法是求余运算. 代码块: #i ...

  6. 【IEEE754制32位浮点数】与十进制相互转换

    IEEE754制32位浮点数转换为十进制 首先了解一下IEEE754制32位浮点数的存储方式 一个由IEEE745制的32位浮点数由三部分组成 组成部分 位数 含义 符号位 1位 0为正,1为负 指数 ...

  7. 利用10位AD转换器 及 LED 数码管,实现由AD转换器采集温度,并用数码管显示。温度超出一定范围,LED 指示灯闪烁报警。(主要分析进制16进制转换10进制,并用数码管显示问题)

    ** 题目: ** 利用原有的电路 AD 及 LED 数码管显示等例子,进行整合.实现由 AD转换器采集温度(用可调电阻模拟),温度在数码管上显示.当温度超出一定范围,用 LED 指示灯进行闪烁报警. ...

  8. 清华大学最早的计算机设计人,厉害了,我的清华简!原来最早的十进制计算器设计得如此巧妙……...

    原标题:厉害了,我的清华简!原来最早的十进制计算器设计得如此巧妙-- 请点击标题下方的"光明微教育"关注我们,了解更多动态~ 吉尼斯世界纪录认证官23日在清华大学宣布,经过独立核实 ...

  9. 揭秘!10+位DBA大神测评华为云DDS增强版实感

    [摘要]数据库.操作系统.中间件并称为核心基础软件,在 IT 软件堆栈中起到中流砥柱的作用.今天运行的绝大多数企业应用软件都离不开数据库的支持. 随着移动互联网.物联网.云计算.大数据等新技术爆发式发 ...

最新文章

  1. C++ 笔记(05)— 变量(变量定义、声明、初始化、extern关键字、变量之间转换)
  2. 解决IDEA报错:Lambda expressions not supported at language level '7'
  3. Azure SQL Database (19) Stretch Database 概览
  4. mysql Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT
  5. BCB 串口控件的使用 TComm
  6. Linux进程编程(PS: exec族函数、system、popen函数)
  7. 区块链到底是什么?和普通人有什么关系?
  8. transferto遇到的问题java.io.FileNotFoundException: C:\Users\Administrator\AppData\Local\Temp
  9. windows Server 2003使用ip安全策略禁止某ip访问服务器的方法
  10. 搜狗输入法不能输入验证码,验证码错误怎么办?
  11. Namebase 给 GitHub 开发者白送钱,是拉新还是投资骗局?
  12. Scala学习笔记(六):Scala程序
  13. 全栈式python工程师培训课程
  14. 6.0系统机器Xposed框架安装经验
  15. 简单 Quartz定时器使用 入门
  16. uni.showToast appd端不起作用
  17. 洛谷 - P1014 [NOIP1999 普及组] Cantor 表 [Java版]
  18. 【懒人笔记】Web前端开发库
  19. EXCEL表格-数据拆分及分列
  20. IOS系统通话录音功能的实现方案

热门文章

  1. 海尔“灯塔”张瑞敏,一把锤子砸出的硬核人生
  2. break continue 关键字 while和do while循环一维数组Day05
  3. 2020年 30K的前端架构面试题总结(持续更新)
  4. 快消品企业如何进行销售管理?
  5. 列表的增删改查:day4
  6. 碰到Maven依赖冲突脑壳疼?这个工具了解一下
  7. 联想电脑什么桌面录屏软件好用?
  8. 计算机文化基础授课计划,计算机文化基础授课计划表
  9. 关于二叉排序树的平均查找长度的计算
  10. Win32K里的死循环