for循环没有运行到最后?

时间:2017-09-24 02:01:09

标签: java loops

我正在进行一项练习,该练习需要一种方法来总结数据中所有电话号码的所有持续时间(即类似于来电显示)。我设法总结了持续时间,但并非所有数据都被打印出来。我一直试图弄清楚为什么会这样,但我无法绕过它。任何帮助将不胜感激。 以下是代码:

//initial code provided
     public static void main(String[] args) {
          String[] phoneNumbers = new String[100];
          int[] callDurations = new int[phoneNumbers.length];
          int size = 0;

          size = addCall(phoneNumbers, callDurations, size, "555-555-5555", 137);
          size = addCall(phoneNumbers, callDurations, size, "555-555-0000", 12);
          size = addCall(phoneNumbers, callDurations, size, "555-555-1234", 26);
          size = addCall(phoneNumbers, callDurations, size, "555-555-8888", 10);
          size = addCall(phoneNumbers, callDurations, size, "555-555-8888", 10);
          size = addCall(phoneNumbers, callDurations, size, "555-555-7777", 10);

       }


       public static int addCall(String[] phoneNumbers, int[] callDurations, int size, String newNumber, int newDuration) {
          if (size >= phoneNumbers.length) {
             System.out.println("Error adding " + newNumber + ": array capacity exceeded.");
          } else {
             phoneNumbers[size] = newNumber;
             callDurations[size] = newDuration;
             size++;
          }

          return size;
       }

//the portion of code that I'm trying to write

        public static void totalDurations(String[] phoneNumbers, int[] 
        callDurations, int size) {
             String[] copyNum = new String[phoneNumbers.length];  
             int[] copyDur = new int[phoneNumbers.length];  
             int newSize = size;  
             int pos = 1;  //counter for next available empty cell
             copyNum[0] = phoneNumbers[0];
             copyDur[0] = callDurations[0];

             for (int i = 0; i < newSize; i++){
               for (int j = 1; j < size; j++){
                 if (copyNum[i] != phoneNumbers[j]){
                   copyNum[i+pos] = phoneNumbers[j];
                   pos++;
                 }
                 else {
                   copyDur[i] += callDurations[j];
                   newSize = newSize -1;
                 }
               }          
             System.out.println(copyNum[i] + ":" + copyDur[i]+ "s");
             }
           }

我当前的输出

Total Durations:
555-555-5555:137s
555-555-0000:12s
555-555-1234:26s
555-555-8888:20s

2 个答案:

答案 0 :(得分:1)

这是一些非常丑陋的代码,你肯定需要在调试器中运行它,但我很清楚错误是在线上

newSize = newSize - 1;

这会减少外部for循环中使用的变量。如果您只通过六个元素中的四个,则表示为其中两个元素输入了else子句。也许你打算减少size呢?

想想你在这里想做什么。使用调试器,以您不期望的方式查看出错的地方。纠正它们。

答案 1 :(得分:1)

首先,代码很难阅读/理解并且容易出错。我会使用类和内置数据类型,如Map和List。

public static class CallDuration {
    String phoneNumber;
    int duration;

    public CallDuration(String phoneNumber, int duration) {
        this.phoneNumber = phoneNumber;
        this.duration = duration;
    }
}

public static void main(String[] args) {
    List<CallDuration> callDurations = new ArrayList<>();
    callDurations.add(new CallDuration("555-555-5555", 137));
    callDurations.add(new CallDuration("555-555-0000", 12));
    callDurations.add(new CallDuration("555-555-1234", 26));
    callDurations.add(new CallDuration("555-555-8888", 10));
    callDurations.add(new CallDuration("555-555-8888", 10));
    callDurations.add(new CallDuration("555-555-7777", 10));

    printCallDuration(callDurations);
}

private static void printCallDuration(List<CallDuration> callDurations) {
    Map<String, Integer> totalCallDurationMap = new HashMap<>();
    for (CallDuration callDuration : callDurations) {
        Integer value = totalCallDurationMap.computeIfAbsent(callDuration.phoneNumber, x -> 0);
        totalCallDurationMap.put(callDuration.phoneNumber, value + callDuration.duration);
    }

    for (Map.Entry<String, Integer> entry : totalCallDurationMap.entrySet()) {
        System.out.println(entry.getKey() + " - " + entry.getValue());
    }
}