我想我有正确的代码来进行转换(感谢用户 Matt Bryant 的帮助),但我现在对如何继续感到困惑。到目前为止,我所学到的如何在主体中键入代码(public static void main(String args[])),但是我得到帮助的这段代码似乎没有进入 main 方法。谁能帮我运行这段代码?:) (另外我有一个错误说我的扫描仪无法解析为类型/变量)。我知道主要方法中没有写任何内容,但这只是因为我不确定此时该做什么。
import java.util.Scanner;
public class romannumeralconversion {
public static void main(String args[]) {
}
public String ToRoman() {
Scanner myKeyboard = new Scanner(System.in);
System.out.println("Enter the integer: ");
number = myKeyboard.nextInt();
myKeyboard.close();
return ToRoman(number);
}
public String ToRoman(int number) {
if ((number < 1 || (number > 3999)))
return "INVALID";
if (number >= 1000)
return "M" + ToRoman(number - 1000);
if (number >= 900)
return "CM" + ToRoman(number - 900);
if (number >= 500)
return "D" + ToRoman(number - 500);
if (number >= 400)
return "CD" + ToRoman(number - 400);
if (number >= 100)
return "C" + ToRoman(number - 100);
if (number >= 90)
return "XC" + ToRoman(number - 90);
if (number >= 50)
return "L" + ToRoman(number - 50);
if (number >= 40)
return "XL" + ToRoman(number - 40);
if (number >= 10)
return "X" + ToRoman(number - 10);
if (number >= 9)
return "IX" + ToRoman(number - 9);
if (number >= 5)
return "V" + ToRoman(number - 5);
if (number >= 4)
return "IV" + ToRoman(number - 4);
if (number >= 1)
return "I" + ToRoman(number - 1);
return "INVALID";
}
}