import java.util.*;
/*不知道为什么,刚刚发的帖子看不到内容,再发一次*/
public class ExchangeNum {
public static void main(String[] args)
{
run();
}
public static void run()
{
Scanner in=new Scanner(System.in);
System.out.print("你想转换进制的数是: ");
int num=in.nextInt();
System.out.println("--------------------------------");
System.out.println("想将你输入的数转换成二进制请输入:1");
System.out.println("想将你输入的数转换成八进制请输入:2");
System.out.println("想将你输入的数转换成十六进制请输入:3");
System.out.println("显示以上所有进制结果请输入:4");
System.out.println("--------退出请输入:0----------");
int index=in.nextInt();
if(index==1)
{
System.out.print("二进制为:");
toBin(num);
System.out.println();
}
if(index==2)
{
System.out.print("八进制为:");
toBa(num);
System.out.println();
}
if(index==3)
{
System.out.print("十六进制为:");
toHex(num);
System.out.println();
}
if(index==4)
toAll(num);
if(index==0)
System.exit(0);
System.out.println("----------------");
System.out.println("| 继续请输入:1 |");
System.out.println("| 退出请输入:0 |");
System.out.println("----------------");
int index2=in.nextInt();
if(index2==1)
run();
if(index==0)
System.exit(0);
}
public static void index()
{
System.out.println("--------------------------------------");
System.out.println("| 想将你输入的数转换成二进制请输入:1 |");
System.out.println("| 想将你输入的数转换成八进制请输入:2 |");
System.out.println("| 想将你输入的数转换成十六进制请输入:3 |");
System.out.println("| 显示以上所有进制结果请输入:4 |");
System.out.println("--------退出请输入:0-------------------");
}
public static void toBin(int a)
{
if(a>0)
toBin(a/2);
System.out.print(a%2);
}
public static void toBa(int a)
{
if(a>0)
toBa(a/8);
System.out.print(a%8);
}
public static void toHex(int a)
{
/*
char[] ch=new char[]={'0','1','2','3','4'
,'5','6','7','8','9',
'A','B','C','D','E'};
*/
if(a>0)
toHex(a/16);
if(a%16<10)
System.out.print(a%16);
else
switch(a%16)
{
case 10:
System.out.print('A');
case 11:
System.out.print('B');
case 12:
System.out.print('C');
case 13:
System.out.print('D');
case 14:
System.out.print('E');
case 15:
System.out.print('F');
}
}
public static void toAll(int a)
{
System.out.print("二进制为:");
toBin(a);
System.out.println();
System.out.print("八进制为:");
toBa(a);
System.out.println();
System.out.print("十六进制为:");
toHex(a);
System.out.println();
}
}
|
|