将数值转换为罗马数字

时间:2014-10-25 18:50:25

标签: java methods roman-numerals

我需要使用不同的方法将数值转换为罗马数字值。以下是我已经创建的代码。

public static void main (String[]args){
    Scanner in = new Scanner(System.in);
    int input = in.nextInt();
    String s = "";
}
private static int promptUserForNumber(Scanner inScanner) {
    System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
    int input = inScanner.nextInt();
    if((input < 1 && input > 3999) && input != 0){
        System.out.println("Error, number must be between 1 and 3999");
         if (input == 0){
            System.out.println("Goodbye");
         }  
        }
    return input;
}
public static String convertNumberToNumeral(int input) {    
    String s = "";
    Scanner in = new Scanner(System.in);
    while (input >= 1000){
        s += "M";
        input -= 1000;
    }
    while (input >= 900){
        s += "CM";
        input -= 900;
    }
    while (input >= 500){
        s += "D";
        input -= 500;
    }
    while (input >= 400){
        s += "CD";
        input -= 400;
    while (input >= 100){
        s += "C";
        input -= 100;
    }
    while (input >= 90){
        s += "XC";
        input -= 90;
    }
    while (input >= 50){
        s += "L";
        input -= 50;
    }
    while (input >= 40){
        s += "XL";
        input -= 40;
    }
    while (input >= 10){
        s += "X";
        input -= 10;
    }
    while (input >= 9){
        s += "IX";
        input -= 10;
    }
    while (input >= 5){
        s += "V";
        input -= 5;
    }
    while (input >= 4){
        s += "IV";
        input -= 4;
        }
    while (input >= 1){
        s += "I";
        input -= 1;
    }
    }
    return s;
}

截至目前,存在无限循环,我不知道我哪里出错了。输出应输入0到3999之间的数值,然后在下一行输出罗马数字。谢谢!

3 个答案:

答案 0 :(得分:1)

    while (input >= 400){
    s += "CD";
    input -= 400;

} 在这里你还需要一个“}”

答案 1 :(得分:1)

我不知道你是否留下了一些代码,但是这个代码不会带你到任何地方,因为主要方法不是调用ohter方法。

您要做的是调用可以这种方式完成的其他方法:

System.out.println(convertNumberToNumeral(promptUserForNumber(in)));

立即打印出答案。 在测试代​​码时,我发现另一个问题,即程序没有正确显示数字3999和2999.问题是:

    while (input >= 500){
        s += "D";
        input -= 500;
    }
    while (input >= 400){
        s += "CD";
        input -= 400;
    } // You forgot this curly bracket and you have 
      // to delete one curly bracket at the end of your method
    while (input >= 100){
        s += "C";
        input -= 100;
    }

您的代码编写得不是很好。有很多未使用的代码。我改成了这个:

public class RomanNumbers{
static String s = "";
static Scanner scan;

public static void main (String[] args){
    scan = new Scanner(System.in);
    System.out.println(convertNumberToNumeral(promptUserForNumber()));
}

private static int promptUserForNumber() {
    System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
    int input = scan.nextInt();

    if(input < 0 && input > 3999){
        System.out.println("Error, number must be between 1 and 3999");
    } else if(input == 0) {
        System.out.println("Goodbye");
    }
    return input;
}

public static String convertNumberToNumeral(int input) {    
    while (input >= 1000){
        s += "M";
        input -= 1000;
    }
    while (input >= 900){
        s += "CM";
        input -= 900;
    }
    while (input >= 500){
        s += "D";
        input -= 500;
    }
    while (input >= 400){
        s += "CD";
        input -= 400;
    }
    while (input >= 100){
        s += "C";
        input -= 100;
    }
    while (input >= 90){
        s += "XC";
        input -= 90;
    }
    while (input >= 50){
        s += "L";
        input -= 50;
    }
    while (input >= 40){
        s += "XL";
        input -= 40;
    }
    while (input >= 10){
        s += "X";
        input -= 10;
    }
    while (input >= 9){
        s += "IX";
        input -= 10;
    }
    while (input >= 5){
        s += "V";
        input -= 5;
    }
    while (input >= 4){
        s += "IV";
        input -= 4;
        }
    while (input >= 1){
        s += "I";
        input -= 1;
    }
    return s;
}
}

答案 2 :(得分:0)

public static void main (String[]args){
    Scanner in = new Scanner(System.in);
    int i = promptUserForNumber(in);
    if (i != -1) {
        //convertNumberToNumeral
    } else {
        //invalid no is entered
    }
}
private static int promptUserForNumber(Scanner inScanner) {
    System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
    int input = inScanner.nextInt();
    if((input < 1 && input > 3999) || input == 0) {
        System.out.println("Error, number must be between 1 and 3999");
         return -1;
    }
    return input;
}

此外,正如Jimmy已经指出的那样,你错过了一个}