java语言编写一个程序,从键盘输入一个整数,将其转换为二进制数并输出 求大神帮忙

如题所述

public class Main{
public static void main(String[] args) {
System.out.println("请输入整数:");
Scanner sc=new Scanner(System.in);
String input = sc.nextLine();//读取输入字符
try{
int i = Integer.parseInt(input);//string转int
String result = Integer.toBinaryString(i);//十进制转二进制
System.out.println("对应二进制数为:");
System.out.println(result);
}catch (Exception e) {//对非法输入做出处理
System.err.println("请按照要求输入~");
}

}
}

主要用了Scanner类读取控制台输入,以及Integer类进制转化

ps:我自己手动写的,为什么被认证为疑似抄袭QWQ

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-09-25
java.lang.Integer这个API包中有进制转换的函数
public static String toBinaryString(int i)
第2个回答  2014-09-25
手机怎么写?
import 几个包,io,string,等
直接用函数转换就可以了追问

你会?

追答

这个帮不了你,我是手机上百度知道,等别人吧

本回答被网友采纳
第3个回答  2014-09-25
追问

相似回答