class ChangeBinary
{
private int i = 0;
private int []a = new int[1024];
public void change(int n)
{
do
{
a[i++] = n%2;
n/=2;
}while(n>0);
for(int j=i-1;j>=0;j--)
System.out.print(a[j]);
}
}
class ChangeNum
{
public static void main(String [] args)
{
int []buf = new int[1024];
int ch = 0;
int pos = 0;
ChangeBinary b = new ChangeBinary();
System.out.println("please input num:");
while(true)
{
try
{ch = System.in.read();}
catch(Exception e)
{System.out.println(e.getMessage());}
switch(ch)
{
case '\r':
break;
case '\n':
for(int i=0;i<pos;i++)
{
System.out.print(buf[i]+" ");
}
System.out.println();
for(int j=0;j<pos;j++)
{
b.change(buf[j]);
System.out.print(" ");
}
System.out.println();
pos = 0;
break;
default:
buf[pos++] = ch;
}
}
}
}
7 个解决方案
#1
int i = 255;
System.out.println(Integer.toBinaryString(i));
System.out.println(Integer.toBinaryString(i));
#2
jdk1.4写法
import java.io.*;
import java.util.*;
public class Main
{
public static void main (String args[]) throws Exception
{
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
String line = stdin.readLine();
try
{
int i = Integer.parseInt(line.trim());
System.out.println(i+"对应的二进制字符串是:"+Integer.toBinaryString(i));
}
catch(Exception e)
{System.out.println("输入整数错误:"+e.getMessage());}
}
}
import java.io.*;
import java.util.*;
public class Main
{
public static void main (String args[]) throws Exception
{
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
String line = stdin.readLine();
try
{
int i = Integer.parseInt(line.trim());
System.out.println(i+"对应的二进制字符串是:"+Integer.toBinaryString(i));
}
catch(Exception e)
{System.out.println("输入整数错误:"+e.getMessage());}
}
}
#3
jdk1.5写法
import java.util.*;
public class Main
{
public static void main(String args[])throws Exception
{
Scanner cin = new Scanner(System.in);
int i = cin.nextInt();
System.out.println(i+"对应的二进制字符串是:"+Integer.toBinaryString(i));
}
}
随手写的,可能有编译错误,自己看着改。大概是这样。
import java.util.*;
public class Main
{
public static void main(String args[])throws Exception
{
Scanner cin = new Scanner(System.in);
int i = cin.nextInt();
System.out.println(i+"对应的二进制字符串是:"+Integer.toBinaryString(i));
}
}
随手写的,可能有编译错误,自己看着改。大概是这样。
#4
up
#5
谢谢,不过作业要求所有方法源代码自己写。
#6
那这样行不行?
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
public static String getString(int i) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << 1;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= 1;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
调用:
getString(2);//得到10
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
public static String getString(int i) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << 1;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= 1;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
调用:
getString(2);//得到10
#7
高人,能加个qq吗?我的 147520916.
#1
int i = 255;
System.out.println(Integer.toBinaryString(i));
System.out.println(Integer.toBinaryString(i));
#2
jdk1.4写法
import java.io.*;
import java.util.*;
public class Main
{
public static void main (String args[]) throws Exception
{
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
String line = stdin.readLine();
try
{
int i = Integer.parseInt(line.trim());
System.out.println(i+"对应的二进制字符串是:"+Integer.toBinaryString(i));
}
catch(Exception e)
{System.out.println("输入整数错误:"+e.getMessage());}
}
}
import java.io.*;
import java.util.*;
public class Main
{
public static void main (String args[]) throws Exception
{
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
String line = stdin.readLine();
try
{
int i = Integer.parseInt(line.trim());
System.out.println(i+"对应的二进制字符串是:"+Integer.toBinaryString(i));
}
catch(Exception e)
{System.out.println("输入整数错误:"+e.getMessage());}
}
}
#3
jdk1.5写法
import java.util.*;
public class Main
{
public static void main(String args[])throws Exception
{
Scanner cin = new Scanner(System.in);
int i = cin.nextInt();
System.out.println(i+"对应的二进制字符串是:"+Integer.toBinaryString(i));
}
}
随手写的,可能有编译错误,自己看着改。大概是这样。
import java.util.*;
public class Main
{
public static void main(String args[])throws Exception
{
Scanner cin = new Scanner(System.in);
int i = cin.nextInt();
System.out.println(i+"对应的二进制字符串是:"+Integer.toBinaryString(i));
}
}
随手写的,可能有编译错误,自己看着改。大概是这样。
#4
up
#5
谢谢,不过作业要求所有方法源代码自己写。
#6
那这样行不行?
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
public static String getString(int i) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << 1;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= 1;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
调用:
getString(2);//得到10
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
public static String getString(int i) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << 1;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= 1;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
调用:
getString(2);//得到10
#7
高人,能加个qq吗?我的 147520916.