编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。下面是我的代码``麻烦高手帮改下吖``比如输入16 他的二进制是10000.后面的零都会在转换为字符串时去掉..则转换结果输出1...哪位高手帮帮忙吖?????public class Test3
{
public static void main(String[] args)
{
byte[] buf = new byte[1024];
String str = null;
String s1 = "";
int pos = 0;
int ch = 0;
int j = 100;
int num = 0;
long e;
int[] x = new int[j];
System.out.println("输入bye退出.\n请输入一个数字: ");
while(true)
{
try {
ch = System.in.read();
} catch (Exception e1) {
e1.printStackTrace();
}
switch (ch) {
case '\r':
break;
case '\n':
str = new String(buf, 0, pos);
if (str.equalsIgnoreCase("bye"))
{
return;
}
else
{
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)<48||str.charAt(i)>58)
{
System.out.print("请输入数字.");
throw new IllegalArgumentException();
}
}
}
if(true)
{
ch = Integer.parseInt(str);
num = ch%2;
for (int i =0;i<x.length ;i++)
{
ch = ch/2;
if (ch == 0)
{
break;
}
x[i] = ch%2;
s1 += x[i];
}
try
{
s1 = num + s1;
e = Long.valueOf(s1).longValue();
System.out.print(e%10);
for (; ; )
{
e = e/10;
if (e == 0)
{
break;
}
System.out.print(e%10);
}
}catch(Exception e2)
{
System.out.println("输入的数字过大.");
e2.printStackTrace();
}
}
pos = 0;
break;
default:
buf[pos++] = (byte) ch;
}
}
}}
{
public static void main(String[] args)
{
byte[] buf = new byte[1024];
String str = null;
String s1 = "";
int pos = 0;
int ch = 0;
int j = 100;
int num = 0;
long e;
int[] x = new int[j];
System.out.println("输入bye退出.\n请输入一个数字: ");
while(true)
{
try {
ch = System.in.read();
} catch (Exception e1) {
e1.printStackTrace();
}
switch (ch) {
case '\r':
break;
case '\n':
str = new String(buf, 0, pos);
if (str.equalsIgnoreCase("bye"))
{
return;
}
else
{
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)<48||str.charAt(i)>58)
{
System.out.print("请输入数字.");
throw new IllegalArgumentException();
}
}
}
if(true)
{
ch = Integer.parseInt(str);
num = ch%2;
for (int i =0;i<x.length ;i++)
{
ch = ch/2;
if (ch == 0)
{
break;
}
x[i] = ch%2;
s1 += x[i];
}
try
{
s1 = num + s1;
e = Long.valueOf(s1).longValue();
System.out.print(e%10);
for (; ; )
{
e = e/10;
if (e == 0)
{
break;
}
System.out.print(e%10);
}
}catch(Exception e2)
{
System.out.println("输入的数字过大.");
e2.printStackTrace();
}
}
pos = 0;
break;
default:
buf[pos++] = (byte) ch;
}
}
}}
public class Dicemal
{
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);
}
}