Java 将整数转换为罗马数字

Java 将整数转换为罗马数字,java,Java,我的代码有问题。请帮忙。这是我到目前为止的代码,我需要使用的方法。它需要能够将整数1-3999转换成罗马数字。有没有比我所做的更简单的方法 public static void main(String[] args) { Scanner in = new Scanner (System.in); System.out.print("Enter a number between 1 and 3999 (0 to quit): "); int input = in.n

我的代码有问题。请帮忙。这是我到目前为止的代码,我需要使用的方法。它需要能够将整数1-3999转换成罗马数字。有没有比我所做的更简单的方法

    public static void main(String[] args) {
    Scanner in = new Scanner (System.in);
    System.out.print("Enter a number between 1 and 3999 (0 to quit): ");
    int input = in.nextInt();
    while (input !=0 )
    {
        if(input < 0 || input > 3999){
        System.out.println("ERROR! NUmber must be between 1 and 3999 (0 to quit): ");
        System.out.print("Enter a number between 1 and 3999 (0 to quit): ");
        input = in.nextInt();
        }
        else if(input > 0){
        String roman = convertNumberToNumeral(input);
        System.out.println("The number " + input + " is the Roman numberal " + roman);
        System.out.print("Enter a number between 1 and 3999 (0 to quit): ");
        input = in.nextInt();
        }
    }
    while (input == 0)
    {
        break;
    }
        System.out.println("Goodbye!");
}

// Given a Scanner as input, prompts the user to input a number between 1 and 3999.
// Checks to make sure the number is within range, and provides an error message until
// the user provides a value within range.  Returns the number input by the user to the
// calling program.
private static int promptUserForNumber(Scanner inScanner, int input) {
}

// Given a number as input, converts the number to a String in Roman numeral format, 
// following the rules in the writeup for Lab 09.  Returns the String to the calling
// program.  NOTE:  This method can possibly get long and complex.  Use the 
// convertDigitToNumeral method below to break this up and make it a bit simpler to code.
private static String convertNumberToNumeral(int input) {
    String romanOnes = ("");
    String romanTens = ("");
    String romanHundreds = ("");
    String romanThousands = ("");
    int ones = input % 10;
    int tens2 = input / 10;
    if (tens2 < 10)
    {
        tens2 = input / 10;
    }
    else {
        tens2 = tens2 % 100;
    }
    int tens = tens2;

    int hundreds2 = input / 100;
    if (hundreds2 < 10)
    {
        hundreds2 = input / 10;
    }
    else {
        hundreds2 = hundreds2 % 1000;
    }

    int hundreds = hundreds2;

    int thousands2 = input / 1000;
    if (thousands2 < 10)
    {
        thousands2 = input / 10;
    }
    else {
        thousands2 = thousands2 % 10000;
    }

    int thousands = input & 10000;
    {
        if (ones == 0)
        {
            romanOnes = ("");
        }
        else if (ones == 1)
        {
            romanOnes = ("I");
        }
        else if (ones == 2)
        {
            romanOnes = ("II");
        }
        else if(ones == 3)
        {
            romanOnes = ("III");
        }
        else if(ones == 4)
        {
            romanOnes = ("IV");
        }
        else if(ones == 5)
        {
            romanOnes = ("V");
        }
        else if(ones == 6)
        {
            romanOnes = ("VI");
        }
        else if(ones == 7)
        {
            romanOnes = ("VII");
        }
        else if(ones == 8)
        {
            romanOnes = ("VIII");
        }
        else if(ones == 9)
        {
            romanOnes = ("IX");
        }
    }
    {
        if (tens == 0)
        {
            romanTens = ("");
        }
        else if (tens == 1)
        {
            romanTens = ("X");
        }
        else if (tens == 2)
        {
            romanTens = ("XX");
        }
        else if(tens == 3)
        {
            romanTens = ("XXX");
        }
        else if(tens == 4)
        {
            romanTens = ("XL");
        }
        else if(tens == 5)
        {
            romanTens = ("L");
        }
        else if(tens == 6)
        {
            romanTens = ("LX");
        }
        else if(tens == 7)
        {
            romanTens = ("LXX");
        }
        else if(tens == 8)
        {
            romanTens = ("LXXX");
        }
        else if(tens == 9)
        {
            romanTens = ("XC");
        }
    }
    {
        if (hundreds == 0)
        {
            romanHundreds = ("");
        }
        else if (hundreds == 1)
        {
            romanHundreds = ("C");
        }
        else if (hundreds == 2)
        {
            romanHundreds = ("CC");
        }
        else if(hundreds == 3)
        {
            romanHundreds = ("CCC");
        }
        else if(hundreds == 4)
        {
            romanHundreds = ("CD");
        }
        else if(hundreds == 5)
        {
            romanHundreds = ("D");
        }
        else if(hundreds == 6)
        {
            romanHundreds = ("DC");
        }
        else if(hundreds == 7)
        {
            romanHundreds = ("DCC");
        }
        else if(hundreds == 8)
        {
            romanHundreds = ("DCCC");
        }
        else if(hundreds == 9)
        {
            romanHundreds = ("CM");
        }
    }
    {
        if (thousands == 0)
        {
            romanThousands = ("");
        }
        else if (thousands == 1)
        {
            romanThousands = ("M");
        }
        else if (thousands == 2)
        {
            romanThousands = ("MM");
        }
        else if(thousands == 3)
        {
            romanThousands = ("MMM");
        }
    }
    String roman = (romanThousands + romanHundreds + romanTens + romanOnes);
    return roman;

}

// Given a digit and the Roman numerals to use for the "one", "five" and "ten" positions,
// returns the appropriate Roman numeral for that digit.  For example, if the number to
// convert is 49 we would call convertDigitToNumeral twice.  The first call would be:
//     convertDigitToNumeral(9, 'I','V','X')
// and would return a value of "IX".  The second call would be:
//     convertDigitToNumeral(4, 'X','L','C')
// and would return a value of "XL".  Putting those togeter we would see that 49 would be the
// Roman numeral XLIX.
// Call this method from convertNumberToNumeral above to convert an entire number into a 
// Roman numeral.
private static String convertDigitToNumeral(int digit, char one, char five, char ten) {


}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入一个介于1和3999之间的数字(0表示退出):”;
int input=in.nextInt();
while(输入!=0)
{
如果(输入<0 | |输入>3999){
System.out.println(“错误!数字必须介于1和3999之间(0才能退出):”;
System.out.print(“输入一个介于1和3999之间的数字(0表示退出):”;
input=in.nextInt();
}
否则如果(输入>0){
字符串罗马=转换器数字(输入);
System.out.println(“数字“+input+”是罗马数字“+Roman”);
System.out.print(“输入一个介于1和3999之间的数字(0表示退出):”;
input=in.nextInt();
}
}
while(输入=0)
{
打破
}
System.out.println(“再见!”);
}
//给定扫描仪作为输入,提示用户输入1到3999之间的数字。
//检查以确保号码在范围内,并提供错误消息,直到
//用户提供范围内的值。将用户输入的数字返回给
//呼叫程序。
私有静态int promptUserForNumber(扫描仪inScanner,int输入){
}
//给定一个数字作为输入,将该数字转换为罗马数字格式的字符串,
//遵循实验室09书面报告中的规则。将字符串返回给调用
//节目。注意:此方法可能会变得冗长而复杂。使用
//下面的ConvertDigitOnumeral方法可以将其分解,并使其编码更简单。
专用静态字符串转换器numbertonumeric(int输入){
字符串romanOnes=(“”);
字符串romanTens=(“”);
字符串为(“”);
字符串=(“”);
整数=输入%10;
int tens2=输入/10;
如果(张力2<10)
{
tens2=输入/10;
}
否则{
tens2=tens2%100;
}
int-tens=tens2;
int hundreds2=输入/100;
如果(百分之二<10)
{
百分之二=输入/10;
}
否则{
百分之二=百分之二的1000;
}
整数百=百分之二;
整数千分之二=输入/1000;
如果(千分之二<10)
{
千分之二=输入/10;
}
否则{
千分之二=千分之二%10000;
}
整数千=输入&10000;
{
如果(一=0)
{
罗马音=(“”);
}
else如果(一=1)
{
罗曼人=(“I”);
}
否则,如果(1==2)
{
罗马币=(“II”);
}
否则,如果(1==3)
{
罗马币=(“III”);
}
否则,如果(1==4)
{
罗马币=(“IV”);
}
否则,如果(1==5)
{
罗马币=(“V”);
}
否则,如果(1==6)
{
罗马币=(“VI”);
}
否则,如果(1==7)
{
罗马币=(“VII”);
}
否则,如果(1==8)
{
罗马币=(“VIII”);
}
否则,如果(1==9)
{
罗马币=(“IX”);
}
}
{
如果(十=0)
{
romanTens=(“”);
}
else if(十=1)
{
romanTens=(“X”);
}
else if(十=2)
{
romanTens=(“XX”);
}
否则如果(十=3)
{
romanTens=(“XXX”);
}
else if(十=4)
{
romanTens=(“XL”);
}
else if(十=5)
{
romanTens=(“L”);
}
else if(十=6)
{
romanTens=(“LX”);
}
else if(十=7)
{
romanTens=(“LXX”);
}
否则如果(十=8)
{
romanTens=(“LXXX”);
}
else if(十=9)
{
romanTens=(“XC”);
}
}
{
如果(百分位==0)
{
罗马数百=(“”);
}
else if(百分位==1)
{
(C);
}
否则如果(百分之=2)
{
罗马币=(“CC”);
}
否则如果(百分之=3)
{
罗马币=(“CCC”);
}
否则如果(百分之=4)
{
罗马币=(“CD”);
}
否则如果(百分之=5)
{
(D);
}
否则如果(百分之=6)
{
罗马币=(“DC”);
}
否则如果(百分之=7)
{
罗曼达=(“DCC”);
}
否则如果(百分之=8)
{
罗曼达=(“DCCC”);
}
否则如果(百分之=9)
{
罗姆人=(“厘米”);
}
}
{
如果(千=0)
{
罗马数=(“”);
}
否则如果(千==1)
{
罗姆人=(“M”);
}
否则如果(千==2)
{
罗马币=(“MM”);
}
否则如果(千==3)
{
罗马币=(“MMM”);
}
}
字符串罗马=(罗马千+罗马百+罗马十+罗马一);
返回罗马;
}
//给定用于“1”、“5”和“10”位置的数字和罗马数字,
//返回该数字的相应罗马数字。例如,如果数字为
//convert是49,我们称之为ConvertDigitOnumeral两次。第一个电话是:
//ConvertDigitOnumeral(9,‘I’、‘V’、‘X’)
//并将返回值“IX”。第二个电话是:
//convertDigitToNumeral(4,'X','L','C')
//并将返回一个值“XL”。把它们放在一起我们就可以
def digit2roman(n, ten, five, one):
  digits = [(9, one+ten), (5, five), (4, one+five), (1, one)]
  result = ""
  while len(digits) > 0:
    val, romn = digits[0]
    if n < val:
      digits.pop(0)
    else:
      n -= val
      result += romn
  return result
def digit2roman(n, ten, five, one):
  digitsV = [9, 5, 4, 1]
  digitsR = [one+ten, five, one+five, one]
  result = ""
  i = 0
  while i < len(digitsV):
    if n < digitsV[i]:
      i += 1
    else:
      n -= digitsV[i]
      result += digitsR[i]
  return result
static HashMap<Character, Integer> numToInt;

public static void setup()
{
    numToInt = new HashMap<Character, Integer>();

// this is my trick to avoid listing the numbers
    String numerals = "MDCLXVI";
    int number = 1000;
    int factor = 2;
    for (char numeral : numerals.toCharArray()) {
        numToInt.put(numeral, number);
        number /= factor;
        factor = (factor == 2 ? 5 : 2);
    }
}

public static int romanNumeralsToInt(String numeralStr)
{
    int total = 0;
    int prevVal = 0;
    for (int i = 0; i < numeralStr.length(); i++) {
        int val = numToInt.get(numeralStr.charAt(i));
        total += val - (prevVal > val && prevVal != 0 ? 0 : 2 * prevVal);
        prevVal = val;
    }
    int len = numeralStr.length();
    return total;
}
string result = ""; int ones = input % 10; int tens = (input /10) % 10; int hundreds = (input / 100) % 10; int thousands = input / 1000; switch (thousands) { case 1: result = "M" break; case 2: result = "MM" break; // etc ... } switch (tens) { case 1: result = result + "C" break; // etc .. } switch (hundreds) { // etc .. } switch (ones) { // etc .. } return result;
private static final String[] digitFormats = new String[] {
  "", "%1$c", "%1$c%1$c", "%1$c%1$c%1$c", "%1$c%2$c",
  "%2$c", "%2$c%1$c", "%2$c%1$c%1$c", "%2$c%1$c%1$c%1$c", "%2$c%3$c"
};

private static String convertDigitToNumeral(int digit, char one, char five, char ten) {
  return String.format(digitFormats[digit], one, five, ten);
}