将罗马数字转换为数字逻辑错误java

时间:2014-10-10 04:18:05

标签: java arrays

所以我有一个家庭作业,我必须能够Roman numeral转换,反之亦然。我理解如何从一个数字到另一个数字,但是数字的字母让我感到困惑。我需要帮助,因为我编写了我的程序和罗马数字来编号。例如,如果我尝试XCIX == 99,我会199。但如果我为2014做罗马数字,那就有效。另外,如果我只输入一个字母,我会得到0。我只需要帮助了解我需要做些什么来解决问题。

import java.util.HashMap;
import java.util.Scanner;

public class Apweek2 {

public static void main(String[] args) {
    Scanner userinput = new Scanner(System.in);
    System.out.print("enter a number to convert to roman numerals: ");
    int input = userinput.nextInt();

    String[] rv = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD",
            "D", "CM", "M" };
    int[] values_for_rv = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900,
            1000 };

    System.out.print("enter a roman numeral: ");
    String roman_numeral = userinput.next();

    int sum = 0;
    String two_spot = null;
    String last_value = null;
    for (int i = 1, j = 0; j < roman_numeral.length()
            && i < roman_numeral.length(); i++, j++) {

        last_value = roman_numeral.substring(j, i);

        char roman_noodles = roman_numeral.charAt(i);
        char raman_noodles = roman_numeral.charAt(j);

        String roman_values = Character.toString(roman_noodles);

        two_spot = last_value + roman_values;

        if (two_spot.contains(rv[1])) {
            sum = sum + values_for_rv[1];
        }
        if (two_spot.contains(rv[3])) {
            sum = sum + values_for_rv[3];
        }
        if (two_spot.contains(rv[5])) {
            sum = sum + values_for_rv[5];
        }
        if (two_spot.contains(rv[7])) {
            sum = sum + values_for_rv[7];
        }
        if (two_spot.contains(rv[9])) {
            sum = sum + values_for_rv[9];
        }
        if (two_spot.contains(rv[11])) {
            sum = sum + values_for_rv[11];
        }

        if (!(two_spot.equals(rv[1])) && !(two_spot.equals(rv[3]))
                && !(two_spot.equals(rv[5])) && !(two_spot.equals(rv[7]))
                && !(two_spot.equals(rv[9])) && !(two_spot.equals(rv[11]))) {

            if (raman_noodles == 'I') {
                sum = sum + 1;
            }
            if (raman_noodles == 'V') {
                sum = sum + 5;
            }
            if (raman_noodles == 'X') {
                sum = sum + 10;
            }
            if (raman_noodles == 'L') {
                sum = sum + 50;
            }
            if (raman_noodles == 'C') {
                sum = sum + 100;
            }
            if (raman_noodles == 'D') {
                sum = sum + 500;
            }
            if (raman_noodles == 'M') {
                sum = sum + 1000;
            }

        }

    }
    System.out.println("converted roman numeral is: " + sum);

    String inputconversion = inputtoroman(input);

    System.out.print("Converted number is: " + inputconversion);
}

public static String inputtoroman(int x) {

    String s1 = "";
    String s2 = "";
    String s3 = "";
    String s4 = "";
    String s5 = "";
    String s6 = "";
    String s7 = "";
    String s8 = "";
    String s9 = "";
    String s10 = "";
    String s11 = "";
    String s12 = "";
    String s13 = "";

    while (x >= 1000) {
        s1 += "M";
        x -= 1000;
    }
    while (x >= 900) {
        s2 += "CM";
        x -= 900;
    }
    while (x >= 500) {
        s3 += "D";
        x -= 500;
    }
    while (x >= 400) {
        s4 += "CD";
        x -= 400;
    }
    while (x >= 100) {
        s5 += "C";
        x -= 100;
    }
    while (x >= 90) {
        s6 += "XC";
        x -= 90;
    }
    while (x >= 50) {
        s7 += "L";
        x -= 50;
    }
    while (x >= 40) {
        s8 += "XL";
        x -= 40;
    }
    while (x >= 10) {
        s9 += "X";
        x -= 10;
    }
    while (x >= 9) {
        s10 += "IX";
        x -= 9;
    }
    while (x >= 5) {
        s11 += "V";
        x -= 5;
    }
    while (x >= 4) {
        s12 += "IV";
        x -= 4;
    }
    while (x >= 1) {
        s13 += "I";
        x -= 1;
    }
    String combined = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10
            + s11 + s12 + s13;
    return combined;

}
}

2 个答案:

答案 0 :(得分:2)

你从1开始,所以当你只输入1个数字时,它根本不会进入循环。应更改为与roman_numeral的全长进行比较。

for (int i = 1, j = 0; j < roman_numeral.length()
        && i <= roman_numeral.length(); i++, j++) {

这可能也会影响它的循环方式,但我现在无法自己运行。

编辑:实际上,再看一遍,我已经看到它会在char roman_noodles = roman_numeral.charAt(i);

上崩溃

如果您只想检查一个字符,则需要添加一些额外的逻辑。

答案 1 :(得分:0)

所以我终于弄明白了我的代码中的问题,当我发现一个匹配的罗马数字中有一个像IV这样的2个罗马数字时,我不得不为整个two_spot增加两次索引。然后我还必须找到最后一个值并计算它,因为for循环只能正确地均衡。然后我还必须再次增加值两次,如果它没有找到像IV这样的值的匹配。因此,我以两对的形式移动字符串,并成对计算两个,除非字符串具有奇数长度。它计算字符串中的最后一个值并继续所有偶数值。最后,如果任何人都需要帮助转换罗马数字,并且是java的初学者,这应该有所帮助。

import java.util.Scanner;

public class Apweek2 {

public static void main(String[] args) {
    Scanner userinput = new Scanner(System.in);
    System.out.print("enter a number to convert to roman numerals: ");
    int input = userinput.nextInt();

    String[] rv = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD",
            "D", "CM", "M" };
    int[] values_for_rv = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900,
            1000 };

    System.out.print("enter a roman numeral: ");
    String roman_numeral = userinput.next().toUpperCase();


    int sum = 0;

    if (roman_numeral.length() % 2 == 0) {

    } else {
        char odd_value = roman_numeral.charAt(roman_numeral.length() - 1);
        if (odd_value == 'I') {
            sum = sum + 1;
        }
        if (odd_value == 'V') {
            sum = sum + 5;
        }
        if (odd_value == 'X') {
            sum = sum + 10;
        }
        if (odd_value == 'L') {
            sum = sum + 50;
        }
        if (odd_value == 'C') {
            sum = sum + 100;
        }
        if (odd_value == 'D') {
            sum = sum + 500;
        }
        if (odd_value == 'M') {
            sum = sum + 1000;
        }
    }

    String two_spot = null;
    String last_value = null;
    for (int i = 1, j = 0; j < roman_numeral.length()
            && i < roman_numeral.length(); i++, j++) {

        last_value = roman_numeral.substring(j, i);

        char roman_noodles = roman_numeral.charAt(i);

        String roman_values = Character.toString(roman_noodles);

        two_spot = last_value + roman_values;

        if (two_spot.contains(rv[1])) {
            sum = sum + values_for_rv[1];
            i++;
            j++;
        }
        if (two_spot.contains(rv[3])) {
            sum = sum + values_for_rv[3];
            i++;
            j++;
        }
        if (two_spot.contains(rv[5])) {
            sum = sum + values_for_rv[5];
            i++;
            j++;
        }
        if (two_spot.contains(rv[7])) {
            sum = sum + values_for_rv[7];
            i++;
            j++;
        }
        if (two_spot.contains(rv[9])) {
            sum = sum + values_for_rv[9];
            i++;
            j++;
        }
        if (two_spot.contains(rv[11])) {
            sum = sum + values_for_rv[11];
            i++;
            j++;
        }

        if (!(two_spot.equals(rv[1])) && !(two_spot.equals(rv[3]))
                && !(two_spot.equals(rv[5])) && !(two_spot.equals(rv[7]))
                && !(two_spot.equals(rv[9])) && !(two_spot.equals(rv[11]))) {

            for (int k = 0; k < two_spot.length(); k++) {

                char raman_noodles = two_spot.charAt(k);

                if (raman_noodles == 'I') {
                    sum = sum + 1;
                }
                if (raman_noodles == 'V') {
                    sum = sum + 5;
                }
                if (raman_noodles == 'X') {
                    sum = sum + 10;
                }
                if (raman_noodles == 'L') {
                    sum = sum + 50;
                }
                if (raman_noodles == 'C') {
                    sum = sum + 100;
                }
                if (raman_noodles == 'D') {
                    sum = sum + 500;
                }
                if (raman_noodles == 'M') {
                    sum = sum + 1000;
                }

            }
            i++;
            j++;
        }

    }
    System.out.println("Converted roman numeral to number is: "+sum);

    String inputconversion = inputtoroman(input);

    System.out.print("Converted number is: " + inputconversion);
}

public static String inputtoroman(int x) {

    String s1 = "";
    String s2 = "";
    String s3 = "";
    String s4 = "";
    String s5 = "";
    String s6 = "";
    String s7 = "";
    String s8 = "";
    String s9 = "";
    String s10 = "";
    String s11 = "";
    String s12 = "";
    String s13 = "";

    while (x >= 1000) {
        s1 += "M";
        x -= 1000;
    }
    while (x >= 900) {
        s2 += "CM";
        x -= 900;
    }
    while (x >= 500) {
        s3 += "D";
        x -= 500;
    }
    while (x >= 400) {
        s4 += "CD";
        x -= 400;
    }
    while (x >= 100) {
        s5 += "C";
        x -= 100;
    }
    while (x >= 90) {
        s6 += "XC";
        x -= 90;
    }
    while (x >= 50) {
        s7 += "L";
        x -= 50;
    }
    while (x >= 40) {
        s8 += "XL";
        x -= 40;
    }
    while (x >= 10) {
        s9 += "X";
        x -= 10;
    }
    while (x >= 9) {
        s10 += "IX";
        x -= 9;
    }
    while (x >= 5) {
        s11 += "V";
        x -= 5;
    }
    while (x >= 4) {
        s12 += "IV";
        x -= 4;
    }
    while (x >= 1) {
        s13 += "I";
        x -= 1;
    }
    String combined = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10
            + s11 + s12 + s13;
    return combined;

  }

}