저는 데이터의 모든 전화 번호에 대해 모든 기간 (즉, 발신자 ID와 비슷한)을 합산하는 방법이 필요한 연습을하고 있습니다. 나는 모든 데이터가 프린트되고있는 것은 아니지만 지속 시간을 요약 해 냈습니다. 나는 이것이 왜 그런지 알아 내려고 노력했지만 그 주위에 내 머리를 감쌀 수는 없다. 어떤 도움이라도 대단히 감사하겠습니다. 아래 코드입니다 :루프가 끝까지 실행되지 않습니까?
//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
IDE의 디버깅 기능에 익숙합니까? –
아니요. 저는 Java에 익숙하지 만 (현재 Java 박사가 운영 중입니다) 디버거 사용에 익숙하지 않았습니다. – sly
그 부분은 강사가 제공하고 제공 한 것입니다. 아니, ArrayLists를 배웠다. – sly