2012-08-10 131 views
-1

enter image description here爪哇 - 使用方法

這是從過去的紙問題編號之間的轉換,我真的不知道該怎麼做。到目前爲止,我已經完成了這項任務,任何幫助都會被讚賞我的代碼可能會產生混淆,因爲我試評我需要什麼,然後做嘗試做

public class NumberConvert { 

public NumberConvert(int from, int to) { 

} 

public String convert(String input) { 

    if(input==null){ 
     return ""; 
    }else{ 
     return "FF";//Not sure what to do here 
    } 

} 

public static Integer valueOf(String s, int radix) 
     throws NumberFormatException { 

    try {//if radix is out of range (not sure what the range is) 
    }catch(NumberFormatException e){ 
     System.out.println(e.getMessage()); 
    } 
    return 0;// to complete 

} 

public static String toString(int i, int radix) { 

    return "";//need to complete 

} 

public static void main (String[] args){ 

} 
} 
+0

您應該輸入問題而不是發佈圖片,因爲這樣不會像索引一樣(以及其他可訪問性問題)。 – WhyNotHugo 2012-08-11 00:29:20

回答

3

Integer類提供了一個版本的parseInt(),允許你指定的基數,同樣的toString()方法還允許你指定基數。

整個轉換可以在一個行完成:

return Integer.toString(Integer.parseInt(input, from), to)); 

其他兩種方法只使用一條線路的部分,或者你可以打電話給你的方法,圍繞整數方法薄包裝。

您需要創建實例字段來存儲fromto並在您的構造函數中設置它們。

+0

謝謝你,我再給它一次 – nsc010 2012-08-10 19:13:28