[英]What is the difference between these two methods? JAVA
我的教授为我提供了一堆填写罗马数字程序的方法(采用加法格式,因此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将如何调用它。
我研究了其他罗马数字程序,但似乎找不到使用其他方法调用的方法,例如我可以将其与之比较的方法。
任何建议表示赞赏!
我假设romanDigitChar返回一个完全匹配数字的char,例如1、5、10、50、100等。 printRomanNumeral将反复调用此方法以获取已知值(即数字)以将其转换为字符。 我建议两个嵌套循环,一个用于减少值中具有特定字符的数量的循环,另一个用于提取每个值。 内部循环调用第二种方法。
我假设他/她期望ASCII字符,尽管罗马数字有特殊的Unicode字符。
对于初学者来说,romanDigitchar返回一个char(罗马数字,对应于作为输入的给定自然数)。 printRomanNumeral不返回任何内容,但是应该打印罗马数字。
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.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.