java之整数和罗马数之间的转换

对于Java中Int类型的整数与罗马字进行转换(String类型),例1可以换成“I”这样,通常使用调用方法(switch方法),对输入进行遍历,进行二者之间的转换。

public class Zhengshuzhuanluoma12 {
    public static void main(String[] args) {
        Zhengshuzhuanluoma12 zhengshuzhuanluoma12 = new Zhengshuzhuanluoma12();
        String res = zhengshuzhuanluoma12.intToRoman(1994);
        System.out.println(res);
    }
    public String intToRoman(int num) {
        String res = "";
        int a = 0;
        int count = 0;
        while (num != 0){
            a = num % 10;
            num = num / 10;
            if (count == 0) {
                res = IntToroman1(a) + res;
            }
            else if(count == 1){
                res = IntToroman2(a) + res;
            }
            else if(count == 2){
                res = IntToroman3(a) + res;
            }
            else {
                res = IntToroman4(a) + res;
            }
            count++;
        }
        return res;
    }
    public String IntToroman1(int n){
        String res = "";
        switch (n){
            case 0: {res = "";break;}
            case 1 : {res = "I";break;}
            case 2 : {res = "II";break;}
            case 3: {res = "III";break;}
            case 4: {res = "IV";break;}
            case 5: {res = "V";break;}
            case 6: {res = "VI";break;}
            case 7: {res = "VII";break;}
            case 8: {res = "VIII";break;}
            case 9: {res = "IX";break;}
        }
        return res;
    }
    public String IntToroman2(int n){
        String res = "";
            switch (n){
                case 0: {res = "";break;}
                case 1 : {res = "X";break;}
                case 2 : {res = "XX";break;}
                case 3: {res = "XXX";break;}
                case 4: {res = "XL";break;}
                case 5: {res = "L";break;}
                case 6: {res = "LX";break;}
                case 7: {res = "LXX";break;}
                case 8: {res = "LXXX";break;}
                case 9: {res = "XC";break;}
            }
            return res;
    }
    public String IntToroman3(int n){
        String res = "";
        switch (n){
            case 0: {res = "";break;}
            case 1 : {res = "C";break;}
            case 2 : {res = "CC";break;}
            case 3: {res = "CCC";break;}
            case 4: {res = "CD";break;}
            case 5: {res = "D";break;}
            case 6: {res = "DC";break;}
            case 7: {res = "DCC";break;}
            case 8: {res = "DCCC";break;}
            case 9: {res = "CM";break;}
        }
        return res;
    }
    public String IntToroman4(int n){
        String res = "";
        switch (n){
            case 1 : {res = "M";break;}
            case 2 : {res = "MM";break;}
            case 3: {res = "MMM";break;}
        }
        return res;
    }
}

public class Luomazhuanzhengshu13 {
    public static void main(String[] args) {
        Luomazhuanzhengshu13 luomazhuanzhengshu13 = new Luomazhuanzhengshu13();
        String s = "MMM";
        int a = luomazhuanzhengshu13.romanToInt(s);
        System.out.println(a);
    }
    public int romanToInt(String s) {
        s = s.replace("IV", "a");
        s = s.replace("IX", "b");
        s = s.replace("XL", "c");
        s = s.replace("XC", "d");
        s = s.replace("CD", "e");
        s = s.replace("CM", "f");
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            res += getvalue(s.charAt(i));
        }
        return res;
    }
    public int getvalue(char g){
        int res1 = 0;
        switch (g){
            case 'I': {res1 = 1;break;}
            case 'V': {res1 = 5;break;}
            case 'X': {res1 = 10;break;}
            case 'L': {res1 = 50;break;}
            case 'C': {res1 = 100;break;}
            case 'D': {res1 = 500;break;}
            case 'M': {res1 = 1000;break;}
            case 'a': {res1 = 4;break;}
            case 'b': {res1 = 9;break;}
            case 'c': {res1 = 40;break;}
            case 'd': {res1 = 90;break;}
            case 'e': {res1 = 400;break;}
            case 'f': {res1 = 900;break;}
        }
        return res1;
    }
}

你可能感兴趣的:(java,算法)