验证罗马数字! (爪哇)

问题描述

我编写了一个 Java 程序来将罗马数字转换为数字。我唯一的问题是,如果有人输入 "IIII",它会显示为 4,但它应该给出一个错误,指出它不是有效的罗马数字。我需要在我的代码中包含以下规则。有人可以帮我吗?

(1) 连续重复的数字不超过三次,即I、X、C重复次数不能超过3次。

(2) 数字 V、L 和 D 不重复。 V、L、D重复构成数无效。

解决方法

罗马数字到十进制数字的转换:

() -> counter + 1;
,

这是我的罗马数字到十进制数字转换器的版本。

这是我的众多测试之一的测试结果。

Enter a Roman numeral: ccccllllxxxxvvvviiii
The input Roman numeral CCCCLLLLXXXXVVVVIIII is invalid.
The correct Roman numeral is DCLXIV.
The decimal value is 664.
Enter a Roman numeral: mcmlxxii
The input Roman numeral MCMLXXII is valid.
The decimal value is 1972.
Enter a Roman numeral: mmmccclll
The input Roman numeral MMMCCCLLL is invalid.
The correct Roman numeral is MMMCDL.
The decimal value is 3450.
Enter a Roman numeral: mcmlxxio
The input Roman numeral MCMLXXIO contains invalid characters.
Enter a Roman numeral: lcl
The input Roman numeral LCL is invalid.
The correct Roman numeral is CC.
The decimal value is 200.
Enter a Roman numeral: quit

基本上,我将输入的罗马数字转换为十进制数。然后我将十进制数转换回罗马数字。我比较了罗马数字,并输出了正确的罗马数字和十进制值。

这是完整的可运行代码。

import java.util.Scanner;

public class RomanNumeralConversion {

    public static void main(String[] args) {
        RomanNumeralConversion rnc = new RomanNumeralConversion();
        rnc.processRomanNumerals();
    }
    
    private Object[][] conversion = { { 1000,900,500,400,100,90,50,40,10,9,5,4,1 },{ "M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I" } };
    
    public void processRomanNumerals() {
        Scanner scanner = new Scanner(System.in);
        String inputRomanNumeral = readRomanNumeral(scanner);
        
        while (!inputRomanNumeral.equals("QUIT")) {
            int value = convertToDecimal(inputRomanNumeral);
            if (value < 0) {
                System.out.println("The input Roman numeral " + 
                        inputRomanNumeral + " contains invalid characters.");
            } else {
                String calculatedRomanNumeral = convertToRoman(value);
                
                if (inputRomanNumeral.equals(calculatedRomanNumeral)) {
                    System.out.println("The input Roman numeral " + 
                            inputRomanNumeral + " is valid.");
                } else {
                    System.out.println("The input Roman numeral " + 
                            inputRomanNumeral + " is invalid.");
                    System.out.println("The correct Roman numeral is " + 
                            calculatedRomanNumeral + ".");
                }
                System.out.println("The decimal value is " + value + ".");
            }
            inputRomanNumeral = readRomanNumeral(scanner);
        }
        
        scanner.close();
    }

    private String readRomanNumeral(Scanner scanner) {
        System.out.print("Enter a Roman numeral: ");
        return scanner.nextLine().trim().toUpperCase();
    }
    
    private int convertToDecimal(String input) {
        int output = 0;
        int index = 0;
        
        while (index < input.length()) {
            boolean isInvalid = true;
            for (int i = 0; i < conversion[1].length; i++) {
                String test = (String) conversion[1][i];
                int j = index + test.length();
                if ((j <= input.length()) && 
                        (input.substring(index,j).equals(test))) {
                    output += (Integer) conversion[0][i];
                    index = j;
                    isInvalid = false;
                    break;
                }
            }
            
            if (isInvalid) {
                return -1;
            }
        }
        
        return output;
    }
    
    private String convertToRoman(int input) {
        String output = "";
        
        for (int i = 0; i < conversion[0].length; i++) {
            int value = (Integer) conversion[0][i];
            if (input >= value) {
                output += (String) conversion[1][i];
                input -= value;
                i--;
            }
        }
    
        return output;
    }

}