-1

在此处输入图像描述

这是过去论文中的一个问题,我不确定该怎么做。到目前为止我已经这样做了,任何帮助表示赞赏。当我尝试评论我需要做的事情然后尝试去做时,我的代码可能会令人困惑

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

}
}
4

1 回答 1

3

该类Integer提供了一个版本parseInt(),允许您指定基数,类似地,该toString()方法还允许您指定基数。

整个转换可以在一行中完成:

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

其他两种方法只使用这一行的一部分,或者您可以将您的方法称为 Integer 方法的薄包装器。

您需要创建实例字段以在构造函数中存储fromto设置这些字段。

于 2012-08-10T18:59:46.210 回答