【问题标题】:Two repeating characters on the same array being checked if it is in the last position. But one of the characters is in the last position检查同一数组上的两个重复字符是否位于最后一个位置。但是其中一个字符在最后一个位置
【发布时间】:2019-10-10 15:55:03
【问题描述】:

这是我第一次使用测试驱动开发,我正在制作一个小型计算器,但使用字符串使其更有趣。然后将字符串分解并计算各个数字。

不幸的是,我在添加两个相同的数字时遇到了这个问题。顺序是 2,3,7,5,3。我的前 3 个与占据阵列中最后一个位置的最后 3 个进行比较。我不断检查该字符是否是数组中的最后一个字符。但我的意思是检查定位而不是实际值本身。

请注意字符串已经被修剪,这意味着它们不包含空格。

在使用数组之前,我使用了 CharacterItorator。不幸的是,我无法管理并决定是否可以通过可信赖的数组得到答案。我设法解决了其他序列,但最后一位数字不重复。

输入字符串:“2,3,7,5,3”。答案应该是 20,但结果是 23

public int inputIterator(String input){
        int currentCount = 0, tempCount;
        String currentString = "";
        char[] chars = input.toCharArray();
        for (char ch : chars){
            if(ch != ','){
                // it is a number
                if(ch == chars[chars.length-1]) {
                    currentString = currentString + ch;
                    tempCount = Integer.parseInt(currentString);
                    currentCount = currentCount + tempCount;
                } else {
                    currentString = currentString + ch;
                }
            } else {
                // It is a ','
                if(ch == chars[chars.length-1]){
                    tempCount = Integer.parseInt(currentString);
                    currentCount = currentCount + tempCount;
                } else {
                    if(currentString != ""){
                        tempCount = Integer.parseInt(currentString);
                        currentCount = currentCount + tempCount;
                        currentString = "";
                    } else {
                        // do nothing
                    }
                }
            }
        }
        return currentCount;
    }

测试期望这个序列的结果是 20。但提供的答案是 23,这是因为重复数字。

编辑:改变了我的逻辑; cmets 中提供的答案确实有效 - 但我仍想尝试自己的旋转

public int inputIterator(String input){
        int currentCount = 0, tempCount;
        String currentString = "";
        int loopCounter = input.length();
        char[] chars = input.toCharArray();
        for(int i = 0; i < loopCounter; i++){
            if(chars[i] != ','){
                // it is a number
                if(i == loopCounter-1) {
                    currentString = currentString + chars[i];
                    tempCount = Integer.parseInt(currentString);
                    currentCount = currentCount + tempCount;
                } else {
                    currentString = currentString + chars[i];
                }
            } else {
                // It is a ','
                if(i == loopCounter-1){
                    tempCount = Integer.parseInt(currentString);
                    currentCount = currentCount + tempCount;
                } else {
                    if(currentString != ""){
                        tempCount = Integer.parseInt(currentString);
                        currentCount = currentCount + tempCount;
                        currentString = "";
                    } else {
                        // do nothing
                    }
                }
            }
        }
        return currentCount;
    }

【问题讨论】:

  • 不确定这与测试有何关系,因为测试似乎做得很好。
  • @jsheeran 你是对的!我会看看如何删除标签。
  • if(ch == chars[chars.length-1]) { 是导致您的问题的行。它在任何时候触发一个字符与chars 中的最后一个字符相同,而不是chars 中的最后一个字符。在不将 for-each 循环的性质更改为 for 循环的情况下解决此问题并不容易,因此我建议改为:return Pattern.compile(",").splitAsStream(input).mapToInt(Integer::parseInt).sum();
  • @Avi 非常感谢。我有点失望,我无法用我的逻辑解决它,但我以前从未想过我会遇到这个问题。
  • @OfficerChalk 您可以在退出循环后立即进行处理,而不是检查字符是否 等于 到最后一个字符。只要您跟踪上次访问的角色,您就会在退出循环时获得该信息。

标签: java arrays string iteration


【解决方案1】:

那么问题一定出在你的逻辑上。你会考虑使用基于流的更短的函数吗?例如:

public int sumFromString(String input) {
    return Arrays.stream(input.split(","))
        .mapToInt(Integer::parseInt)
        .sum();
}

【讨论】:

    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多