gpt4 book ai didi

java - 超过金额限制后消息不显示

转载 作者:行者123 更新时间:2023-12-01 17:46:44 25 4
gpt4 key购买 nike

当我输入超过 3999 的数字时,消息不显示

我尝试在方法内移动消息,但这只会导致更多错误。

import java.util.Scanner;

public class RomanNumerals {

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;

}

我希望用户输入超过 3999 后的输出显示消息“数字无法转换,因为它超过 3999”,但是当我写入超过 3999 的数字时,程序就会停止。其他一切工作正常并以罗马数字显示正确的整数

有人可以帮我解决这个问题吗,因为我很困惑

最佳答案

这是一个典型的案例,说明您可能会因为缩进不良而错过发现问题的机会。

以下elseif (units >= 0)而不是和if ((n >= 0) && (n <= 3999))在一起:

} else {
roman = "Number cannot be converted as it exceeds over 3999";
}

另外,在一个不相关的注释中,您在 if (units >= 0) 中的情况始终为 true,因此您可能需要删除或更改此检查。

编辑:完整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;
}

关于java - 超过金额限制后消息不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54357438/

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