public static void main(String[] args) throws Exception
{
int shang=0,yu;
boolean flag=false;
System.out.println("请输入一个数字(最大值为<"+Integer.MAX_VALUE+"):");
InputStream is = System.in;
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String str=br.readLine();
char[] ch=new char[str.length()];
str.getChars(0, str.length(), ch, 0);
for(int a=0;a<str.length();a++)
{
if(!Character.isDigit(ch[a]))
{
System.out.println("有非数字字符");
System.exit(0);
}
}
try
{
shang=Integer.parseInt(str);
flag=true;
}
catch(Exception e)
{
System.out.println("数字过大");
}
str="";
if (flag)
{
while (shang != 0)
{
yu = shang % 2;
shang = shang / 2;
str = yu + str;
}
}
System.out.println(str);
}
|
|