我输入的数字超过3999时,消息不会显示
我试图将消息移到方法内部,但这只会导致更多错误。
Edit
}
我希望用户输入超过3999的输出后显示消息“由于超过3999,所以无法转换数字”,但是当我输入超过3999的数字时,程序就会停止。其他一切工作正常,并以罗马数字显示正确的整数
因为我很困惑,有人可以帮我吗
答案 0 :(得分:4)
由于缩进不良而无法看到问题的经典案例。
以下else
与if (units >= 0)
一起使用,而不是if ((n >= 0) && (n <= 3999))
:
} else {
roman = "Number cannot be converted as it exceeds over 3999";
}
另外,在不相关的注释上,您在if (units >= 0)
中的情况始终为真,因此您可能希望删除此支票或对其进行更改。
编辑:修复右括号后的完整convert
方法:
public static String convert(int n) {
String roman = "";
if ((n >= 0) && (n <= 3999)) {
int thousand = (n / 1000);
int hundred = (n % 1000) / 100;
int tens = (n % 100) / 10;
int units = (n % 10);
if (thousand >= 1) {
if (thousand == 1)
roman += "M";
else if (thousand == 2)
roman += "MM";
else roman += "MMM";
}
if (hundred >= 0) {
if (hundred == 0)
roman += "";
else if (hundred == 1)
roman += "C";
else if (hundred == 2)
roman += "CC";
else if (hundred == 3)
roman += "CCC";
else if (hundred == 4)
roman += "CD";
else if (hundred == 5)
roman += "D";
else if (hundred == 6)
roman += "DC";
else if (hundred == 7)
roman += "DCC";
else if (hundred == 8)
roman += "DCCC";
else
roman += "CM";
}
if (tens >= 0) {
if (tens == 0)
roman += "";
else if (tens == 1)
roman += "X";
else if (tens == 2)
roman += "XX";
else if (tens == 3)
roman += "XXX";
else if (tens == 4)
roman += "XL";
else if (tens == 5)
roman += "L";
else if (tens == 6)
roman += "LX";
else if (tens == 7)
roman += "LXX";
else if (tens == 8)
roman += "LXXX";
else
roman += "XC";
}
if (units >= 0) {
if (units == 0)
roman += "";
else if (units == 1)
roman += "I";
else if (units == 2)
roman += "II";
else if (units == 3)
roman += "III";
else if (units == 4)
roman += "IV";
else if (units == 5)
roman += "V";
else if (units == 6)
roman += "VI";
else if (units == 7)
roman += "VII";
else if (units == 8)
roman += "VIII";
else
roman += "IX";
}
} else {
roman = "Number cannot be converted as it exceeds over 3999";
}
return roman;
}