gpt4 book ai didi

java - 将数值转换为罗马数字

转载 作者:行者123 更新时间:2023-12-02 05:20:20 24 4
gpt4 key购买 nike

我需要使用不同的方法将数值转换为罗马数字值。下面是我已经创建的代码。

public static void main (String[]args){
Scanner in = new Scanner(System.in);
int input = in.nextInt();
String s = "";
}
private static int promptUserForNumber(Scanner inScanner) {
System.out.print ("Enter a number between 1 and 3999 (0 to quit): ");
int input = inScanner.nextInt();
if((input < 1 && input > 3999) && input != 0){
System.out.println("Error, number must be between 1 and 3999");
if (input == 0){
System.out.println("Goodbye");
}
}
return input;
}
public static String convertNumberToNumeral(int input) {
String s = "";
Scanner in = new Scanner(System.in);
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 -= 10;
}
while (input >= 5){
s += "V";
input -= 5;
}
while (input >= 4){
s += "IV";
input -= 4;
}
while (input >= 1){
s += "I";
input -= 1;
}
}
return s;
}

到目前为止,有一个无限循环,我不知道哪里出了问题。输出应该是输入 0 到 3999 之间的数值,然后在下一行输出罗马数字。谢谢!

最佳答案

    while (input >= 400){
s += "CD";
input -= 400;

}这里还需要一个“}”

关于java - 将数值转换为罗马数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26565977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com