【发布时间】:2013-11-06 13:55:35
【问题描述】:
计算完两个数字后,如何将整数更改为罗马数字。我不知道如何将整数值更改为等效的罗马数字值。
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 ' ';
}
}
}
【问题讨论】:
-
你知道你有什么单位 - 这与将时间以秒为单位转换为以小时/分钟/秒为单位的时间完全相同。
-
this 是我在网上找到的一个类。可能是您正在寻找的。span>
标签: java