Java实现复杂的进制转换器功能示例
程序员文章站
2024-03-08 18:39:16
本文实例讲述了java实现复杂的进制转换器功能。分享给大家供大家参考,具体如下:
这是用java写的进制转换器,包括10、2、8、16进制之间共12种的相互转换。输入一个...
本文实例讲述了java实现复杂的进制转换器功能。分享给大家供大家参考,具体如下:
这是用java写的进制转换器,包括10、2、8、16进制之间共12种的相互转换。输入一个要转换的数之后,按提示选择所采用的转换方式,输出转换的结果。
注:新上传的文件是在此前代码(下面的3. 原java代码)的基础上做了改进,能更好地实现封装。
1. systemconvert.java
package day8; import java.util.scanner; public class systemconvert { static scanner scanner = new scanner(system.in); static string s = ""; // 定义10进制转2进制的方法。 public static string c10t2(int numb) { string result = ""; for (int i = numb; i > 0; i /= 2) result = i % 2 + result; return result; } // 定义10进制转8进制的方法。 public static string c10t8(int numb) { string result = ""; for (int i = numb; i > 0; i /= 8) result = i % 8 + result; return result; } // 定义2进制转10进制的方法。 public static int c2t10(int numb) { int k = 0, result = 0; // string result=null; for (int i = numb; i > 0; i /= 10) { result += (i % 10) * math.pow(2, k); k++; } return result; } // 定义8进制转10进制的方法。 public static int c8t10(int numb) { int k = 0, temp = 0; for (int i = numb; i > 0; i /= 10) { temp += (i % 10) * math.pow(8, k); k++; } return temp; } public static void convert10(int numb, int to) { string s = ""; switch (to) { case 2: s = "" + c10t2(numb); break; case 8: s = "" + c10t8(numb); break; case 16: s = integer.tohexstring(numb).touppercase(); break; default: system.out.println("wrong input!"); } system.out.println(s); } public static void convert2(int numb, int to) { string s = ""; switch (to) { case 10: s = "" + c2t10(numb); break; case 8: s = "" + c10t8(c2t10(numb)); break; case 16: s = integer.tohexstring(c2t10(numb)).touppercase(); break; default: system.out.println("wrong input!"); } system.out.println(s); } public static void convert8(int numb, int to) { string s = ""; switch (to) { case 2: s = "" + c10t2(c8t10(numb)); break; case 10: s = "" + c8t10(numb); break; case 16: s = integer.tohexstring(c8t10(numb)).touppercase(); break; default: system.out.println("wrong input!"); } system.out.println(s); } public static void convert16(string numb, int to) { string s = ""; switch (to) { case 2: int temp2 = integer.parseint(numb, 16); s = c10t2(temp2); break; case 8: int temp3 = integer.parseint(numb, 16); s = c10t8(temp3); break; case 10: int temp = integer.parseint(numb, 16); s = "" + temp; break; default: system.out.println("wrong input!"); } system.out.println(s); } public static void convert(int numb, int from, int to) { switch (from) { case 10: convert10(numb, to); break; case 2: convert2(numb, to); break; case 8: convert8(numb, to); break; default: system.out.println("wrong input!"); } } public static void convert(string numb, int from, int to) { switch (from) { case 16: convert16(numb, to); break; default: system.out.println("wrong input!"); } } public static void main(string[] args) { system.out.println("要转的是16进制数吗?\n输入1。代表是;\n输入2.代表不是.\n"); int input = scanner.nextint(); switch (input) { case 1: system.out.println("请输入一个16进制数:"); string numb = scanner.next(); system.out.println("转成什么进制的数?"); int to = scanner.nextint(); convert(numb, 16, to); break; case 2: system.out.println("请输入一个数:"); int numb2 = scanner.nextint(); system.out.println("从什么进制数转起?"); int from = scanner.nextint(); system.out.println("转成什么进制的数?"); int to2 = scanner.nextint(); convert(numb2, from, to2); break; default: system.out.println("wrong input!"); } } }
2. 运行效果截图:
3. 原java代码
import java.util.scanner; public class systemconvert { static scanner scanner = new scanner(system.in); static string s = ""; public static void convert() { system.out.println("please input a number:"); string numb = scanner.next(); system.out.println("choose a way:\n输入1,表示10进制转2进制;\n" + "输入2,表示2进制转10进制;\n" + "输入3,表示10进制转8进制;\n" + "输入4,表示8进制转10进制;\n" + "输入5,表示10进制转16进制;\n" + "输入6,表示16进制转10进制;\n" + "输入7,表示2进制转8进制;\n" + "输入8,表示2进制转16进制;\n" + "输入9,表示8进制转2进制;\n" + "输入10,表示8进制转16进制;\n" + "输入11,表示16进制转2进制;\n" + "输入12,表示16进制转8进制;\n"); int input = scanner.nextint(); switch (input) { case 1: // 10>>>2 s = "" + c10t2(numb); break; case 2: // 2>>>10 s += c2t10(numb); break; case 3: // 10>>>8 s = "" + c10t8(numb); break; case 4: // 8>>>10 s = "" + c8t10(numb); break; case 5: // 10>>>16 s = integer.tohexstring(integer.valueof(numb)).touppercase(); break; case 6:// 16>>>10 int temp = integer.parseint(numb, 16); s = "" + temp; break; case 7: // 2>>>8 s = "" + c10t8(integer.tostring(c2t10(numb))); break; case 8: // 2>>>16 s = integer.tohexstring(integer.valueof(c2t10(numb))).touppercase(); break; case 9: // 8>>>2 s = "" + c10t2(integer.tostring(c8t10(numb))); break; case 10:// 8>>>16 s = integer.tohexstring(integer.valueof(c8t10(numb))).touppercase(); break; case 11:// 16>>>2 int temp2 = integer.parseint(numb, 16); s = integer.tobinarystring(temp2); break; case 12:// 16>>>8 int temp3 = integer.parseint(numb, 16); s = c10t8(integer.tostring(temp3)); break; default: system.out.println("wrong input!"); } system.out.println(s); } public static int c2t10(string numb) { int k = 0, result = 0; // string result=null; for (int i = integer.valueof(numb); i > 0; i /= 10) { result += (i % 10) * math.pow(2, k); k++; } return result; } public static int c8t10(string numb) { int k = 0, temp = 0; for (int i = integer.valueof(numb); i > 0; i /= 10) { temp += (i % 10) * math.pow(8, k); k++; } return temp; } public static string c10t8(string numb) { string result = ""; for (int i = integer.valueof(numb); i > 0; i /= 8) result = i % 8 + result; return result; } public static string c10t2(string numb) { string result = ""; for (int i = integer.valueof(numb); i > 0; i /= 2) result = i % 2 + result; return result; } public static void main(string[] args) { systemconvert.convert(); } }
4. 运行截图:
ps:这里再为大家推荐几款本站的在线进制转换与计算工具,相信对于大家能有所帮助:
在线任意进制转换工具:
在线标准计算器:
在线科学计算器:
希望本文所述对大家java程序设计有所帮助。