简体   繁体   English

这两种方法有什么区别? 爪哇

[英]What is the difference between these two methods? JAVA

My prof provided me with a bunch of methods to fill in for a roman numeral program (in additive format, so 4=IIII, 9=VIIII, etc.) 我的教授为我提供了一堆填写罗马数字程序的方法(采用加法格式,因此4 = IIII,9 = VIIII等)

I'm having trouble understanding what the difference is between these two methods: 我无法理解这两种方法之间的区别:

   **
   * 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)

    {

    }

Is romanDigitChar supposed to read a number digit by digit, and only return one digit at a time? romanDigitChar是否应该逐位读取一个数字,并且一次只能返回一个数字? If so, I don't understand how printRomanNumeral would go about calling it. 如果是这样,我不明白printRomanNumeral将如何调用它。

I've researched other roman numeral programs, but I can't seem to find any that use methods called within other methods like this one that I could compare it to. 我研究了其他罗马数字程序,但似乎找不到使用其他方法调用的方法,例如我可以将其与之比较的方法。

Any advice is appreciated! 任何建议表示赞赏!

I assume romanDigitChar returns one char for an exact matching numbers eg 1, 5, 10, 50, 100 etc only. 我假设romanDigitChar返回一个完全匹配数字的char,例如1、5、10、50、100等。 printRomanNumeral would call this repeatedly for known values as numbers to turn them into characters. printRomanNumeral将反复调用此方法以获取已知值(即数字)以将其转换为字符。 I suggest two nested loops, one for amounts with specific characters in decreasing value and one extracting each value. 我建议两个嵌套循环,一个用于减少值中具有特定字符的数量的循环,另一个用于提取每个值。 The inner loop calls the second method. 内部循环调用第二种方法。

I assume he/she expects ASCII characters although there are special Unicode characters for Roman numerals. 我假设他/她期望ASCII字符,尽管罗马数字有特殊的Unicode字符。

Well for starters, romanDigitchar returns a char (the Roman Numeral corresponding to the natural number given as input). 对于初学者来说,romanDigitchar返回一个char(罗马数字,对应于作为输入的给定自然数)。 printRomanNumeral doesn't return anything, but is supposed to print the Roman numeral. printRomanNumeral不返回任何内容,但是应该打印罗马数字。

Is romanDigitChar supposed to read a number digit by digit, and only return one digit at a time? Yes, for instance if you want to print two roman numeral digits: IIII, VIIII. 是的,例如,如果您要打印两个罗马数字:IIII,VIIII。 In your void printRomanNumeral(int val) method,you need to do: 在您的void printRomanNumeral(int val)方法中,您需要执行以下操作:

public void printRomanNumeral(int val)
{
         System.out.println(romanDigitChar(4));
         System.out.println(romanDigitChar(9));          
}

But in your char romanDigitChar(int val) method, you need to have some sorts of algorithm to convert natural number to roman number, something like: 但是在您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM