罗马数字到数字转换

尝试编写程序以读取表示罗马数字(来自用户输入)的字符串,然后将其转换为阿拉伯语forms(整数)。 例如,I = 1,V = 5,X = 10等。

基本上,采用String类型参数的构造函数必须将字符串(来自用户输入)解释为罗马数字并将其转换为相应的int值。

除了正在进行的下面(还没有编译)之外,有没有更简单的方法来解决这个问题:

import java.util.Scanner; public class RomInt { String roman; int val; void assign(String k) { roman=k; } private class Literal { public char literal; public int value; public Literal(char literal, int value) { this.literal = literal; this.value = value; } } private final Literal[] ROMAN_LITERALS = new Literal[] { new Literal('I', 1), new Literal('V', 5), new Literal('X', 10), new Literal('L', 50), new Literal('C', 100), new Literal('D', 500), new Literal('M', 1000) }; public int getVal(String s) { int holdValue=0; for (int j = 0; j < ROMAN_LITERALS.length; j++) { if (s.charAt(0)==ROMAN_LITERALS[j].literal) { holdValue=ROMAN_LITERALS[j].value; break; } //if() }//for() return holdValue; } //getVal() public int count() { int count=0; int countA=0; int countB=0; int lastPosition = 0; for(int i = 0 ; i < roman.length(); i++) { String s1 = roman.substring(i,i+1); int a=getVal(s1); countA+=a; } for(int j=1;jc) { countB+=c; } } count=countA-(2*countB); return count; } void disp() { int result=count(); System.out.println("Integer equivalent of "+roman+" = " +result); } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Please enter Roman Symbol:"); String s = keyboard.nextLine(); RomInt(); } } 

罗马数字/解码示例:

 class Roman { private static int decodeSingle(char letter) { switch (letter) { case 'M': return 1000; case 'D': return 500; case 'C': return 100; case 'L': return 50; case 'X': return 10; case 'V': return 5; case 'I': return 1; default: return 0; } } public static int decode(String roman) { int result = 0; String uRoman = roman.toUpperCase(); //case-insensitive for (int i = 0; i < uRoman.length() - 1; i++) {//loop over all but the last character if (decodeSingle(uRoman.charAt(i)) < decodeSingle(uRoman.charAt(i + 1))) { result -= decodeSingle(uRoman.charAt(i)); } else { result += decodeSingle(uRoman.charAt(i)); } } result += decodeSingle(uRoman.charAt(uRoman.length() - 1)); return result; } public static void main(String[] args) { System.out.println(decode("MCMXC")); //1990 System.out.println(decode("MMVIII")); //2008 System.out.println(decode("MDCLXVI")); //1666 } } 

使用enum ,轻松简单的解决方案。 首先在罗马定义十进制当量权重。

 enum Roman{ i(1),iv(4),v(5), ix(9), x(10); int weight; private Roman(int weight) { this.weight = weight; } }; 

这是将十进制转换为罗马字符串的方法。

 static String decToRoman(int dec){ String roman=""; Roman[] values=Roman.values(); for (int i = values.length-1; i>=0; i--) { while(dec>=values[i].weight){ roman+=values[i]; dec=dec-values[i].weight; } } return roman; } 

您可以尝试使用Hashmap存储罗马数字和等效的阿拉伯数字。

HashMap test = new HashMap();

 test.add("I",1); test.add("V",5); test.add("X",10); test.add("L",50); test.add("C",100); test.add("D",500); test.add("M",1000); //This would insert all the roman numerals as keys and their respective arabic numbers as values. 

要检索用户输入的相应阿拉伯数字,您可以使用以下代码:

 Scanner sc = new Scanner(System.in); System.out.println(one.get(sc.next().toUpperCase())); //This would print the respective value of the selected key.This occurs in O(1) time. 

其次,

如果您只有这些罗马数字集,那么您可以选择简单的switch case语句。

 switch(sc.next().toUpperCase()) { case 'I' : System.out.println("1"); break; case 'V' System.out.println("5"); break; . . . & so on } 

希望这可以帮助。

这个怎么样:

 public static int convertFromRoman(String roman) { Map v = new HashMap(); v.put("IV", 4); v.put("IX", 9); v.put("XL", 40); v.put("CD", 400); v.put("CM", 900); v.put("C", 100); v.put("M", 1000); v.put("I", 1); v.put("V", 5); v.put("X", 10); v.put("L", 50); v.put("D", 500); int result = 0; for (String s : v.keySet()) { result += countOccurrences(roman, s) * v.get(s); roman = roman.replaceAll(s, ""); } return result; } public static int countOccurrences(String main, String sub) { return (main.length() - main.replace(sub, "").length()) / sub.length(); } 

不确定我是否已经拥有所有可能的组合,因为我不是罗马数字的专家。 只需确保您减去的位置在地图中排在第一位。

您可以使用以下代码解决编译问题。 但肯定不是优化的:

 public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Please enter Roman Symbol:"); String s = keyboard.nextLine(); RomInt temp = new RomInt(); temp.getVal(s); temp.assign(s); temp.disp(); }