【问题标题】:How to get mantissa and exponent value separately如何分别获得尾数和指数值
【发布时间】:2021-06-26 12:20:47
【问题描述】:

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

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>

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 文件如下所示:

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();
    }
}

【问题讨论】:

  • 为什么不用“e”分隔逗号之间的每个项目,将第一部分解析为尾数,将第二部分解析为指数?这直接给你一个ScientificNumber
  • 1.2345e9应该如何存储?作为ScientificNumber(12345, 9) 还是ScientificNumber(12345, 5)?或者是其他东西?或者字符串中的数字永远不会有小数点?
  • @Andreas,还没有真正考虑过,因为所有给定的值都只是整数
  • @daniu 你能给我一个代码示例吗,我现在有点迷路了:)
  • Double.intValue() 方法实现为return (int)value;,因此结果无论如何都是强制转换。创建Double 对象只是为了避免编写演员是愚蠢的!

标签: java arrays arraylist hashmap exponent


【解决方案1】:

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

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)));
}

当然是相反的:

@Override
public String toString() {
    return this.mantissa + "e" + this.exponent;
}

【讨论】:

  • 你的意思是 ScientificNumber.java ?我不能更改 ScientificNumber.java,因为我不允许这样做。 ://
  • @TheFoxterGirl 好的,那就不要了。不管你把代码放在哪里,方法的逻辑都是一样的。
猜你喜欢
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 2021-04-26
  • 2013-03-19
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多