Java 如何将整数值更改为罗马数字

Java 如何将整数值更改为罗马数字,java,Java,在我计算完这两个数字后,如何将整数改为罗马数字。我想不出如何将整数值更改为其等效的罗马数字值 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 Seco

在我计算完这两个数字后,如何将整数改为罗马数字。我想不出如何将整数值更改为其等效的罗马数字值

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 ' ';
    }
}
}
import java.util.Scanner;
/**
*要求用户输入两个数字以及罗马数字中的“+”符号,以查找数字的总和。
*  
*@作者(哈普里特·辛格)
*布兰顿百年中学
*@版本(05/11/2013)
*/
公共类计算器
{
公共静态整数总计=0;
公共静态void main(字符串参数[])
{
扫描仪标准输入=新扫描仪(System.in);
System.out.print(“输入罗马数字:>”;
char[]roman=stdIn.nextLine().toCharArray();
对于(int i=roman.length-1;i>-1;i--)
{
开关(罗马[i])
{
案例“I”:
总+=价值(罗马[i]);中断;
案例“V”:
案例“X”:
案例“L”:
案例“C”:
案例“D”:
案例“M”:
如果(i!=0&(值(罗马[i-1])<值(罗马[i]))
{
总+=价值(罗马[i])-价值(罗马[i-1]);
我--;
}
其他的
{
总额+=价值(罗马[i]);
}
打破
}
}        
如果(总数>1000)
{
系统输出打印项次(总计);
System.out.println(“错误非法输入!”);
系统出口(0);
}
System.out.println(“总计为:“+Total”);
}
公共静态int值(字符c)
{
开关(c)
{
案例“I”:
返回1;
案例“V”:
返回5;
案例“X”:
返回10;
案例“L”:
返回50;
案例“C”:
返回100;
案例“D”:
返回500;
案例“M”:
返回1000;
违约:
返回“”;
}
}
}
对于这种有用的方法:

public static String IntegerToRomanNumeral(int input)
{
    if (input < 1 || input > 3999)
        throw new InvalidInputException();

    String s = "";

    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 -= 9;
    } while (input >= 5) {
        s += "V";
        input -= 5;
    } while (input >= 4) {
        s += "IV";
        input -= 4;
    } while (input >= 1) {
        s += "I";
        input -= 1;
    }

    return s;
}
public静态字符串integertoromanumeral(int输入)
{
如果(输入<1 | |输入>3999)
抛出新的InvalidInputException();
字符串s=“”;
同时(输入>=1000){
s+=“M”;
输入-=1000;
}同时(输入>=900){
s+=“厘米”;
输入-=900;
}同时(输入>=500){
s+=“D”;
输入-=500;
}而(输入>=400){
s+=“CD”;
输入-=400;
}同时(输入>=100){
s+=“C”;
输入-=100;
}同时(输入>=90){
s+=“XC”;
输入-=90;
}同时(输入>=50){
s+=“L”;
输入-=50;
}同时(输入>=40){
s+=“XL”;
输入-=40;
}同时(输入>=10){
s+=“X”;
输入-=10;
}while(输入>=9){
s+=“IX”;
输入-=9;
}while(输入>=5){
s+=“V”;
输入-=5;
}while(输入>=4){
s+=“IV”;
输入-=4;
}while(输入>=1){
s+=“I”;
输入-=1;
}
返回s;
}
对于这种有用的方法:

public static String IntegerToRomanNumeral(int input)
{
    if (input < 1 || input > 3999)
        throw new InvalidInputException();

    String s = "";

    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 -= 9;
    } while (input >= 5) {
        s += "V";
        input -= 5;
    } while (input >= 4) {
        s += "IV";
        input -= 4;
    } while (input >= 1) {
        s += "I";
        input -= 1;
    }

    return s;
}
public静态字符串integertoromanumeral(int输入)
{
如果(输入<1 | |输入>3999)
抛出新的InvalidInputException();
字符串s=“”;
同时(输入>=1000){
s+=“M”;
输入-=1000;
}同时(输入>=900){
s+=“厘米”;
输入-=900;
}同时(输入>=500){
s+=“D”;
输入-=500;
}而(输入>=400){
s+=“CD”;
输入-=400;
}同时(输入>=100){
s+=“C”;
输入-=100;
}同时(输入>=90){
s+=“XC”;
输入-=90;
}同时(输入>=50){
s+=“L”;
输入-=50;
}同时(输入>=40){
s+=“XL”;
输入-=40;
}同时(输入>=10){
s+=“X”;
输入-=10;
}while(输入>=9){
s+=“IX”;
输入-=9;
}while(输入>=5){
s+=“V”;
输入-=5;
}while(输入>=4){
s+=“IV”;
输入-=4;
}while(输入>=1){
s+=“I”;
输入-=1;
}
返回s;
}
公共静态整数转换罗马数字(字符串罗马){
字符串modRoman=roman.replace(“CM”、“DCCCC”);
modRoman=modRoman.替换(“CD”、“CCCC”);
modRoman=modRoman.替换(“XC”、“Lxxx”);
modRoman=modRoman.替换(“XL”,“XXXX”);
modRoman=modRoman.替换(“IX”、“VIIIII”);
modRoman=modRoman.替换(“IV”、“IIII”);
映射符号=新的HashMap();
符号。放置('M',1000);
符号。放置('D',500);
符号。放置('C',100);
符号。放置('L',50);
符号。放置('X',10);
符号。放置('V',5);
符号。放置('I',1);
char[]chars=modRoman.toCharArray();
int-total=0;
for(char c:chars){
总数+=符号。获取(c);
}
返回总数;
}
如果你想经常调用它,我会将
符号的初始化移到它自己的方法中,但是将它们全部保留在一个方法中会使在这里发布变得更简单(IMHO)。

公共静态int convertRomanNumeric(字符串roman){
字符串modRoman=roman.replace(“CM”、“DCCCC”);
modRoman=modRoman.替换(“CD”、“CCCC”);
modRoman=modRoman.替换(“XC”、“Lxxx”);
modRoman=modRoman.替换(“XL”,“XXXX”);
modRoman=modRoman.替换(“IX”、“VIIIII”);
modRoman=modRoman.替换(“IV”、“IIII”);
映射符号=新的HashMap();
符号。放置('M',1000);
符号。放置('D',500);
符号。放置('C',100);
符号。放置('L',50);
符号。放置('X',10);
符号。放置('V',5);
符号。放置('I',1);
char[]chars=modRoman.toCharArray();
int-total=0;
for(char c:chars){
总数+=符号。获取(c);
}
返回总数;
}

如果你想经常调用它,我会将
符号的初始化移到它自己的方法中,但是将所有的符号都保留在一个方法中会使在这里发布变得更简单(IMHO)。

你知道你有什么单位-这与转换完全相同