package com.chapter02;import java.util.Scanner;public class SysConvert { /** * other system convert into ten system * * @param a * @param str * d */ public static int otherToTen(int a, String str) { double d = 0; // number after convert String subString; // loop acquire single element on the basis of the // length of the string for (int i = 0; i < str.length(); i++) { subString = str.substring(i, i + 1); // loop cut the string if (a == 16) { subString = sixteenToNumber(subString); // convert the char to // number } d += Integer.parseInt(subString) * Math.pow(a, str.length() - i - 1); } return (int) d; } /** * convert ten system to other system * * @param a * @param str * */ public static String tenToNumber(int a, String str) { int current = Integer.parseInt(str); String opResult = ""; // Judge whether the number after convert is sixteen system if (a == 16) // Judge whether the current number is bigger than sixteen. // If it is,when meet sixteen,carry a number 1 { while (current >= a) { opResult += sixteenNumberToChar(current % a); current /= a; } if (current != 0) opResult += sixteenNumberToChar(current); } else { // Judge whether the current number is bigger than the system after // convert while (current >= a) { opResult += current % a; current /= a; } if (current != 0) opResult += current; } String riResult = ""; // binary system string of inverted order // loop print out by binary system for (int i = opResult.length() - 1; i >= 0; i--) { riResult = riResult + opResult.substring(i, i + 1); } return riResult; } public static String sixteenToNumber(String s) { String num = ""; if (s.equals("A") && s.equals("a")) { num = "10"; } else if (s.equals("B") && s.equals("b")) { num = "11"; } else if (s.equals("C") && s.equals("c")) { num = "12"; } else if (s.equals("D") && s.equals("d")) { num = "13"; } else if (s.equals("E") && s.equals("e")) { num = "14"; } else if (s.equals("F") && s.equals("f")) { num = "15"; } else { num = s; } return num; } public static String sixteenNumberToChar(int num) { // sixteenNumber convert to char String c = ""; if (num == 10) { c = "A"; } else if (num == 11) { c = "B"; } else if (num == 12) { c = "C"; } else if (num == 13) { c = "D"; } else if (num == 14) { c = "E"; } else if (num == 15) { c = "F"; } else { c = String.valueOf(num); } return c; } /** * main,the entry of this program * * @param args */ public static void main(String[] args) { String number; // the number about to convert int a, b; // a means the number before convert // b means the number after convert String result; //the result after convert String stop; Scanner read = new Scanner(System.in); do{ System.out.println("Input three interger numbers:the number before convert,pre system,later system"); number = read.next(); a = read.nextInt(); b = read.nextInt(); stop = "Quit"; }while(stop != "Quit"); try{ if(a != 10) //Judge whether the number before convert is ten system { String temp = String.valueOf(otherToTen(a,number)); //the number convert to ten system result = String.valueOf(tenToNumber(b, temp)); //ten system convert to other system } else { result = String.valueOf(tenToNumber(b,number)); //ten system convert to other system } System.out.println(a + " system's number " + number + " convert to " + b + " system,\nthe result is " + result); }catch(Exception e) { System.out.println("Failed to convert,please input correctly!"); System.exit(-1); } }}