计算完两个数字后,如何将整数更改为罗马数字。我不知道如何将整数值更改为其等效的罗马数字值。
import java.util.Scanner;
/**
* Asks the user to input two numbers along with the "+" sign in roman numerals to find the sum of the numbers.
*
* @author (Harpreet Singh)
* Brampton Centennial Secondary School
* @version (05/11/2013)
*/
public class RomanCalculator
{
public static int total = 0;
public static void main(String args[])
{
Scanner stdIn = new Scanner(System.in);
System.out.print("Enter a Roman Number:> ");
char[] roman = stdIn.nextLine().toCharArray();
for(int i = roman.length-1; i > -1; i--)
{
switch(roman[i])
{
case 'I':
total += value(roman[i]); break;
case 'V':
case 'X':
case 'L':
case 'C':
case 'D':
case 'M':
if(i != 0 && (value(roman[i-1]) < value(roman[i])))
{
total += value(roman[i]) - value(roman[i-1]);
i--;
}
else
{
total += value(roman[i]);
}
break;
}
}
if (total>1000)
{
System.out.println(total);
System.out.println("ERROR ILLEGAL ENTRY!");
System.exit(0);
}
System.out.println("The Total Is: "+total);
}
public static int value(char c)
{
switch(c)
{
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return ' ';
}
}
}