java十进制数转换成二进制,8进制以及16进制二进制转十进制
发布网友
发布时间:2022-04-29 16:40
我来回答
共1个回答
热心网友
时间:2023-10-20 05:40
直接运行得了
import java.io.*;
public class test
{
public static void main(String arg[])
{
test a=new test();
System.out.print("请输入一个十进制整数:");
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i=Integer.parseInt(br.readLine());
System.out.print("该数的二进制数是:");
a.BinaryString(i);
System.out.println();
System.out.print("该数的八进制数是:");
a.OctalString(i);
System.out.println();
System.out.print("该数的十六进制数是:");
a.HexString(i);
System.out.println();
System.out.println(Integer.valueOf("101101",2).toString());//2进制转换为10进制
}catch(IOException e)
{
System.err.println(e.toString());
}
}
public void BinaryString(int x)
{
if(x>0)
BinaryString(x/2);
else
return;
System.out.print(x%2);
}
public void OctalString(int x)
{
if(x>0)
OctalString(x/8);
else
return;
System.out.print(x%8);
}
public void HexString(int x)
{
if(x>0)
BinaryString(x/16);
else
return;
if(x%16==10)
{
System.out.print("A");
}
else if(x%16==11)
{
System.out.print("B");
}
else if(x%16==12)
{
System.out.print("C");
}
else if(x%16==13)
{
System.out.print("D");
}
else if(x%16==14)
{
System.out.print("E");
}
else if(x%16==15)
{
System.out.print("F");
}
else
System.out.print(x%16);
}
}
热心网友
时间:2023-10-20 05:40
直接运行得了
import java.io.*;
public class test
{
public static void main(String arg[])
{
test a=new test();
System.out.print("请输入一个十进制整数:");
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i=Integer.parseInt(br.readLine());
System.out.print("该数的二进制数是:");
a.BinaryString(i);
System.out.println();
System.out.print("该数的八进制数是:");
a.OctalString(i);
System.out.println();
System.out.print("该数的十六进制数是:");
a.HexString(i);
System.out.println();
System.out.println(Integer.valueOf("101101",2).toString());//2进制转换为10进制
}catch(IOException e)
{
System.err.println(e.toString());
}
}
public void BinaryString(int x)
{
if(x>0)
BinaryString(x/2);
else
return;
System.out.print(x%2);
}
public void OctalString(int x)
{
if(x>0)
OctalString(x/8);
else
return;
System.out.print(x%8);
}
public void HexString(int x)
{
if(x>0)
BinaryString(x/16);
else
return;
if(x%16==10)
{
System.out.print("A");
}
else if(x%16==11)
{
System.out.print("B");
}
else if(x%16==12)
{
System.out.print("C");
}
else if(x%16==13)
{
System.out.print("D");
}
else if(x%16==14)
{
System.out.print("E");
}
else if(x%16==15)
{
System.out.print("F");
}
else
System.out.print(x%16);
}
}
热心网友
时间:2023-10-20 05:40
直接运行得了
import java.io.*;
public class test
{
public static void main(String arg[])
{
test a=new test();
System.out.print("请输入一个十进制整数:");
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i=Integer.parseInt(br.readLine());
System.out.print("该数的二进制数是:");
a.BinaryString(i);
System.out.println();
System.out.print("该数的八进制数是:");
a.OctalString(i);
System.out.println();
System.out.print("该数的十六进制数是:");
a.HexString(i);
System.out.println();
System.out.println(Integer.valueOf("101101",2).toString());//2进制转换为10进制
}catch(IOException e)
{
System.err.println(e.toString());
}
}
public void BinaryString(int x)
{
if(x>0)
BinaryString(x/2);
else
return;
System.out.print(x%2);
}
public void OctalString(int x)
{
if(x>0)
OctalString(x/8);
else
return;
System.out.print(x%8);
}
public void HexString(int x)
{
if(x>0)
BinaryString(x/16);
else
return;
if(x%16==10)
{
System.out.print("A");
}
else if(x%16==11)
{
System.out.print("B");
}
else if(x%16==12)
{
System.out.print("C");
}
else if(x%16==13)
{
System.out.print("D");
}
else if(x%16==14)
{
System.out.print("E");
}
else if(x%16==15)
{
System.out.print("F");
}
else
System.out.print(x%16);
}
}
热心网友
时间:2023-10-20 05:40
直接运行得了
import java.io.*;
public class test
{
public static void main(String arg[])
{
test a=new test();
System.out.print("请输入一个十进制整数:");
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i=Integer.parseInt(br.readLine());
System.out.print("该数的二进制数是:");
a.BinaryString(i);
System.out.println();
System.out.print("该数的八进制数是:");
a.OctalString(i);
System.out.println();
System.out.print("该数的十六进制数是:");
a.HexString(i);
System.out.println();
System.out.println(Integer.valueOf("101101",2).toString());//2进制转换为10进制
}catch(IOException e)
{
System.err.println(e.toString());
}
}
public void BinaryString(int x)
{
if(x>0)
BinaryString(x/2);
else
return;
System.out.print(x%2);
}
public void OctalString(int x)
{
if(x>0)
OctalString(x/8);
else
return;
System.out.print(x%8);
}
public void HexString(int x)
{
if(x>0)
BinaryString(x/16);
else
return;
if(x%16==10)
{
System.out.print("A");
}
else if(x%16==11)
{
System.out.print("B");
}
else if(x%16==12)
{
System.out.print("C");
}
else if(x%16==13)
{
System.out.print("D");
}
else if(x%16==14)
{
System.out.print("E");
}
else if(x%16==15)
{
System.out.print("F");
}
else
System.out.print(x%16);
}
}
热心网友
时间:2023-10-20 05:40
直接运行得了
import java.io.*;
public class test
{
public static void main(String arg[])
{
test a=new test();
System.out.print("请输入一个十进制整数:");
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i=Integer.parseInt(br.readLine());
System.out.print("该数的二进制数是:");
a.BinaryString(i);
System.out.println();
System.out.print("该数的八进制数是:");
a.OctalString(i);
System.out.println();
System.out.print("该数的十六进制数是:");
a.HexString(i);
System.out.println();
System.out.println(Integer.valueOf("101101",2).toString());//2进制转换为10进制
}catch(IOException e)
{
System.err.println(e.toString());
}
}
public void BinaryString(int x)
{
if(x>0)
BinaryString(x/2);
else
return;
System.out.print(x%2);
}
public void OctalString(int x)
{
if(x>0)
OctalString(x/8);
else
return;
System.out.print(x%8);
}
public void HexString(int x)
{
if(x>0)
BinaryString(x/16);
else
return;
if(x%16==10)
{
System.out.print("A");
}
else if(x%16==11)
{
System.out.print("B");
}
else if(x%16==12)
{
System.out.print("C");
}
else if(x%16==13)
{
System.out.print("D");
}
else if(x%16==14)
{
System.out.print("E");
}
else if(x%16==15)
{
System.out.print("F");
}
else
System.out.print(x%16);
}
}
热心网友
时间:2023-10-20 05:40
直接运行得了
import java.io.*;
public class test
{
public static void main(String arg[])
{
test a=new test();
System.out.print("请输入一个十进制整数:");
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i=Integer.parseInt(br.readLine());
System.out.print("该数的二进制数是:");
a.BinaryString(i);
System.out.println();
System.out.print("该数的八进制数是:");
a.OctalString(i);
System.out.println();
System.out.print("该数的十六进制数是:");
a.HexString(i);
System.out.println();
System.out.println(Integer.valueOf("101101",2).toString());//2进制转换为10进制
}catch(IOException e)
{
System.err.println(e.toString());
}
}
public void BinaryString(int x)
{
if(x>0)
BinaryString(x/2);
else
return;
System.out.print(x%2);
}
public void OctalString(int x)
{
if(x>0)
OctalString(x/8);
else
return;
System.out.print(x%8);
}
public void HexString(int x)
{
if(x>0)
BinaryString(x/16);
else
return;
if(x%16==10)
{
System.out.print("A");
}
else if(x%16==11)
{
System.out.print("B");
}
else if(x%16==12)
{
System.out.print("C");
}
else if(x%16==13)
{
System.out.print("D");
}
else if(x%16==14)
{
System.out.print("E");
}
else if(x%16==15)
{
System.out.print("F");
}
else
System.out.print(x%16);
}
}