首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何分别获取尾数和指数值

如何分别获取尾数和指数值
EN

Stack Overflow用户
提问于 2021-03-30 21:17:30
回答 1查看 47关注 0票数 0

我已经看到了类似的问题,但仍然没有找到我的问题的答案。我需要做的是,我有一个字符串数组:

代码语言:javascript
复制
private String[] scientificNumberData = "2e3, 2e4, 6e3".split(", "); //[2e3, 2e4, 6e3]

我需要分离尾数和指数才能返回List<ScientificNumber>。为此,我尝试创建Map,其中键表示尾数和值列表指数,因为我可能有重复的键,无法以其他方式将它们分开。

在这段代码中,我的地图看起来像这样:{2=[3, 4, 3], 6=[3, 4, 3]},但应该是2=[3, 4], 6=[3]}

有没有更好的解决方案,或者我可以以某种方式修复我的代码,以便获得用于List<ScientificNumber>的正确输出

代码语言:javascript
复制
public List<ScientificNumber> getScientificNumbers() {

        List<ScientificNumber> result = new LinkedList<>();
        Map<Integer, List<Integer>> separateExponent = new LinkedHashMap<>();
        List<Integer> exs = new LinkedList<>();

        int mantissa = 0;
        int exponent = 0;

        for(String str: scientificNumberData){
            for (int i = 0; i < str.length(); i++){
                if(str.charAt(i) != 'e'){
                    if(matiss == 0){
                        if(!(separateExponent.containsKey(str.charAt(i)))){
                            mantissa = Integer.parseInt(String.valueOf(str.charAt(i)));
                            separateExponent.put(mantissa, exs);
                        }else{
                            mantissa = Integer.parseInt(String.valueOf(str.charAt(i)));
                        }
                    }else{
                        exponent = Integer.parseInt(String.valueOf(str.charAt(i)));
                    }
                }
            }
            // if there is a duplicate key already
            if(separateExponent.containsKey(mantissa)){
                separateExponent.get(mantissa).add(exponent);
            }else {
                // if not duplicate key
                separateExponent.put(mantissa, Collections.singletonList(exponent));
            }
             // change back to default values
            mantissa = 0;
            exponent = 0;
        }
        // works, but since wrong output the total will be wrong
        for(Map.Entry<Integer, List<Integer>> entry : separateExponent.entrySet()){
            for(int i = 0; i < entry.getValue().size(); i++){
                result.add(new ScientificNumber(entry.getKey(), entry.getValue().get(i)));
            }
        }
        return result;
    }

ScientificNumber.java文件如下所示:

代码语言:javascript
复制
public class ScientificNumber {
    private int mantissa;
    private int exponent;

    public ScientificNumber(int mantissa, int exponent) {
        this.mantissa = mantissa;
        this.exponent = exponent;
    }

    public int intValue() {
        return mantissa * Double.valueOf(Math.pow(10, exponent)).intValue();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-03-30 21:26:56

将以下方法添加到ScientificNumber类中:

代码语言:javascript
复制
public static ScientificNumber parse(String str) {
    int idx = str.indexOf('e');
    if (idx == -1)
        return new ScientificNumber(Integer.parseInt(str), 0);
    return new ScientificNumber(Integer.parseInt(str.substring(0, idx)),
                                Integer.parseInt(str.substring(idx + 1)));
}

当然,反之亦然:

代码语言:javascript
复制
@Override
public String toString() {
    return this.mantissa + "e" + this.exponent;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66871694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档