我的教授给我提供了一些方法来填充一个罗马数字程序(以加法格式,例如4=IIII、9=VIIII等)。
我很难理解这两种方法之间的区别:
**
* This method prints, to the standard output and in purely additive
* notation, the Roman numeral corresponding to a natural number.
* If the input value is 0, nothing is printed. This
* method must call the method romanDigitChar().
* @param val ?
*/
public void printRomanNumeral(int val)
{
}
**
* This method returns the Roman numeral digit corresponding
* to an integer value. You must use a nested-if statement.
* This method cannot perform any arithmetic operations.
* @param val ?
* @return ?
*/
public char romanDigitChar(int val)
{
}romanDigitChar是否应该一位一位地读取一个数字,并且一次只返回一个数字?如果是这样的话,我不明白printRomanNumeral将如何调用它。
我研究过其他的罗马数字程序,但我似乎找不到任何使用其他方法调用的方法,比如这个方法,我可以将它与之进行比较。
如有任何建议,敬请谅解!
发布于 2013-10-27 19:38:07
我假设romanDigitChar只返回一个精确匹配数字的一个字符,例如1、5、10、50、100等。printRomanNumeral会将已知值反复调用为数字,以便将它们转换为字符。我建议两个嵌套循环,一个用于具有递减值的特定字符的数量,另一个用于提取每个值。内环调用第二个方法。
我猜想他/她希望使用ASCII字符,尽管对于罗马数字有一些特殊的Unicode字符。
发布于 2013-10-27 19:41:42
首先,romanDigitchar返回一个字符(与作为输入的自然数相对应的罗马数字)。printRomanNumeral不返回任何东西,但应该打印罗马数字。
发布于 2013-10-27 19:43:27
Is romanDigitChar supposed to read a number digit by digit, and only return one digit at a time?是的,例如,如果您想打印两个罗马数字数字: IIII,VIIII。在void printRomanNumeral(int val)方法中,您需要执行以下操作:
public void printRomanNumeral(int val)
{
System.out.println(romanDigitChar(4));
System.out.println(romanDigitChar(9));
}但是在您的char romanDigitChar(int val)方法中,需要有一些算法来将自然数转换为罗马数字,比如:
public char romanDigitChar(int val)
{
if(val == 4) {
//Return a roman digit 4.
}
if(val == 9) {
//Return a roman digit 9.
}
}https://stackoverflow.com/questions/19622510
复制相似问题