进制转换
程序员文章站
2022-07-13 13:23:47
...
package c;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class JinZhiTest
{
static String string;
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static void fun(int num,int n) throws IOException
{
boolean flag=true;
int []b =new int[8];
int count=0;
while(flag)
{
int a=num%n;
num/=n;
b[count]=a;
count++;
if(num/n==1||num/n==0)
{
b[count]=num/2;
flag=false;
}
}
for(int i=7;i>=0;i--)
{
System.out.print(b[i]+" ");
}
}
public static void main(String[] args) throws IOException
{
System.out.println("请输入你要转换的数:");
String s=br.readLine();
int number=Integer.parseInt(s);
System.out.println(number+"的二进制表示形式是:"+Integer.toBinaryString(number));
System.out.print(number+"的三进制表示形式是:");
fun(number,3);
System.out.println();
System.out.print(number+"的五进制表示形式是:");
fun(number,5);
System.out.println();
System.out.println(number+"的八进制表示形式是:"+Integer.toOctalString(number));
System.out.println(number+"的十进制表示形式是:"+number);
System.out.println(number+"的十六进制表示形式是:"+Integer.toHexString(number));
}
}