JAVA中的十进制转换为二进制

不要用函数... 不要用函数 展开
 我来答
百度网友efae51f
推荐于2018-03-13 · TA获得超过2221个赞
知道大有可为答主
回答量:6916
采纳率:0%
帮助的人:3174万
展开全部
public class test {
public static void main(String[] args) {
String m = Integer.toBinaryString(120);
System.out.println(m);
}
}
--------------------------------

public class test {
public static void main(String[] args) {
int m = 13;
String s = "";
while (m > 0) {
s = m % 2 + s;
m = m / 2;
}
System.out.println(s);
}
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yunwuyunwu88
2010-12-07 · 超过27用户采纳过TA的回答
知道答主
回答量:72
采纳率:0%
帮助的人:55.1万
展开全部
public class tobin {
public static void main(String[] args) {
int x = 17;//转换前的十进制数
int num=0;
for(int y=x;y!=0;num++)y=y/2;//计算转换后二进制数的位数
int[] bin = new int[num];//转换后的二进制数
for(int i=num-1,y=x;i>=0;i--){
bin[i]=y%2;
y=y/2;
}
for(int i=0;i<num;i++)
System.out.print(bin[i]);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2010-12-07
展开全部
public class Bin
{

public static void main(String[] args)
{
StringBuffer sbf = toBin(5);
String str=sbf.reverse().toString();
System.out.println(str);
}
static StringBuffer toBin(int x)
{
StringBuffer result=new StringBuffer();
do{
result.append(x%2);
x/=2;
}while(x>0);
return result;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
nizhihe
2010-12-07 · TA获得超过1637个赞
知道小有建树答主
回答量:542
采纳率:73%
帮助的人:173万
展开全部
public static void main(String[] args) {
StringBuffer bi = new StringBuffer();
int x = 90;
do{
int y = x%2;
bi.insert(0,y+"");
x = (x - y) / 2;
}while(x > 0);
System.out.println(bi.toString());
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
皮小皮Cn
2020-12-10 · TA获得超过8866个赞
知道答主
回答量:5.6万
采纳率:3%
帮助的人:3287万
展开全部
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(4)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式