import java.lang.Integer;
import java.util.Scanner;
public class Jsq {
public static void main(String args[]) {
while (true) {
Menu a = new Menu();
a.showMenuInfo();
a.showMenuFun();
}
}
}
class Jisuanqi {
private String s;
public static void tishi1(String ch) {
System.out.print("请输入一个" + ch + "进制数:");
}
public void jinggao(String ch) {
System.err.println("输入有误!您必须输入一个" + ch + "进制数。");
}
public void Two_Eight() {
String m = null;
System.out.println();
System.out.println("二进制——>八进制:");
System.out.println();
tishi1("二");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.toString(Integer.parseInt(s, 2), 8);
} catch (Exception e) {
jinggao("二");
}
System.out.println("转换结果为:" + m);
}
public void Two_Ten() {
int m = 0;
System.out.println();
System.out.println("二进制——>十进制:");
System.out.println();
tishi1("二");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.parseInt(s, 2);
} catch (Exception e) {
jinggao("二");
}
System.out.println("转换结果为:" + m);
}
public void Two_Sixteen() {
String m = null;
System.out.println();
System.out.println("二进制——>十六进制:");
System.out.println();
tishi1("二");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.toString(Integer.parseInt(s, 2), 16);
} catch (Exception e) {
jinggao("二");
}
System.out.println("转换结果为:" + m);
}
public void Eight_Two() {
String m = null;
System.out.println();
System.out.println("八进制——>二进制:");
System.out.println();
tishi1("八");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.toString(Integer.parseInt(s, 8), 2);
} catch (Exception e) {
jinggao("八");
}
System.out.println("转换结果为:" + m);
}
public void Eight_Ten() {
int m = 0;
System.out.println();
System.out.println("八进制——>十进制:");
System.out.println();
tishi1("八");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.parseInt(s, 8);
} catch (Exception e) {
jinggao("八");
}
System.out.println("转换结果为:" + m);
}
public void Eight_Sixteen() {
String m = null;
System.out.println();
System.out.println("八进制——>十六进制:");
System.out.println();
tishi1("八");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.toString(Integer.parseInt(s, 8), 16);
} catch (Exception e) {
jinggao("八");
}
System.out.println("转换结果为:" + m);
}
public void Ten_Two() {
String m = null;
System.out.println();
System.out.println("十进制——>二进制:");
System.out.println();
tishi1("十");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.toString(Integer.parseInt(s, 10), 2);
} catch (Exception e) {
jinggao("十");
}
System.out.println("转换结果为:" + m);
}
public void Ten_Eight() {
String m = null;
System.out.println();
System.out.println("十进制——>八进制:");
System.out.println();
tishi1("十");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.toString(Integer.parseInt(s, 10), 8);
} catch (Exception e) {
jinggao("十");
}
System.out.println("转换结果为:" + m);
}
public void Ten_Sixteen() {
String m = null;
System.out.println();
System.out.println("十进制——>十六进制:");
System.out.println();
tishi1("十");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.toString(Integer.parseInt(s, 10), 16);
} catch (Exception e) {
jinggao("十");
}
System.out.println("转换结果为:" + m);
}
public void Sixteen_Two() {
String m = null;
System.out.println();
System.out.println("十六进制——>二进制:");
System.out.println();
tishi1("十六");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.toString(Integer.parseInt(s, 16), 2);
} catch (Exception e) {
jinggao("十六");
}
System.out.println("转换结果为:" + m);
}
public void Sixteen_Eight() {
String m = null;
System.out.println();
System.out.println("十六进制——>八进制:");
System.out.println();
tishi1("十六");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.toString(Integer.parseInt(s, 16), 8);
} catch (Exception e) {
jinggao("十六");
}
System.out.println("转换结果为:" + m);
}
public void Sixteen_Ten() {
int m = 0;
System.out.println();
System.out.println("十六进制——>十进制:");
System.out.println();
tishi1("十六");
try {
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
m = Integer.parseInt(s, 16);
} catch (Exception e) {
jinggao("十六");
}
System.out.println("转换结果为:" + m);
}
}
class Menu {
private boolean flag = true;
Jisuanqi J = new Jisuanqi();
public void showTitle(String l, String m, String n) {
System.out.println(" " + l + ".将" + m + "进制数转换为" + n + "进制数 ");
}
public void showMenuInfo() {
System.out.println();
System.out.println(" ★欢迎使用★ ");
System.out.println();
System.out.println("==============小计算器==============");
showTitle("(1)", "二", "八");
showTitle("(2)", "二", "十");
showTitle("(3)", "二", "十六");
showTitle("(4)", "八", "二");
showTitle("(5)", "八", "十");
showTitle("(6)", "八", "十六");
showTitle("(7)", "十", "二");
showTitle("(8)", "十", "八");
showTitle("(9)", "十", "十六");
showTitle("(10)", "十六", "二");
showTitle("(11)", "十六", "八");
showTitle("(12)", "十六", "十");
System.out.println(" (0).退出");
System.out.println("====================================");
}
public void askUser() {
char c;
System.out.print("\n是否继续?(Y/N):");
Scanner sc = new Scanner(System.in);
c = sc.nextLine().charAt(0);
if (c == 'y' || c == 'Y') {
flag = true;
} else if (c == 'n' || c == 'N') {
flag = false;
} else {
System.out.println("非法输入!");
flag = false;
}
}
public void showMenuFun() {
int n = 0;
System.out.print("请选择(0——12):");
try {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
} catch (Exception e) {
System.err.println("输入有误!请您重新选择。");
flag = false;
}
while (flag) {
switch (n) {
case 1:
J.Two_Eight();
askUser();
break;
case 2:
J.Two_Ten();
askUser();
break;
case 3:
J.Two_Sixteen();
askUser();
break;
case 4:
J.Eight_Two();
askUser();
break;
case 5:
J.Eight_Ten();
askUser();
break;
case 6:
J.Eight_Sixteen();
askUser();
break;
case 7:
J.Ten_Two();
askUser();
break;
case 8:
J.Ten_Eight();
askUser();
break;
case 9:
J.Ten_Sixteen();
askUser();
break;
case 10:
J.Sixteen_Two();
askUser();
break;
case 11:
J.Sixteen_Eight();
askUser();
break;
case 12:
J.Sixteen_Ten();
askUser();
break;
case 0:
System.exit(0);
break;
default:
System.out.println("输入有误!请您重新选择。");
flag = false;
break;
}
}
}
}