I have an if statement that should not be called if two numbers are the same (utilizing the != function), and I have the body print the two numbers. When the if statement is called, it literally prints that the 2 numbers are the same, so it shouldn't be called. To test, I added another if statement inside of that if statement that was the exact OPPOSITE of the parent if statement, and they BOTH got called. I'm not really sure why this is happening. I'll post the entire function, and for reference I'm trying to calculate mode for a data set, where there can be multiple modes. Also, we're not allowed to use for loops for this, so I have to use while loops in its place, sorry about that.
public static void calcMode(int[] data) {
int answer[] = new int[100]; //The final answer
int maxCount = 0; //Stores the maximum counted number (if there are 3 twos, this value will be 3
int i = 0; //While loop counter
int modeCounter = 0;
while (i < data.length) { //Should be a for loop 6 and the Half Blood Prince
int count = 0; //counts how many numbers match the current number selected
int j = 0; //Second while loop counter
while (j < data.length) { //Should be a for loop 7 - Steel Ball Run
if (data[j] == data[i]) //Checks if current selection (j) is the same as the above selection (i)
count++; //Increases count if the numbers are the same
j++; //Wouldn't be here if this were a for loop 7 - Steel Ball Run
}
if (count > maxCount) { //If the count was higher than the highest count of the loop (this would make it the current mode)
modeCounter = 0;
maxCount = count; //Increases max count to make sure any double number can't overtake a triple number, as an example, it ensures the mode remains the mode
int counter = 0;
while (counter < answer.length) { //Should be a for loop 1 - Remastered
answer[counter] = INVALID; //Sets invalids
counter++; //Wouldn't be here if this were a for loop 1 - Remastered
}
answer[modeCounter] = data[i]; //Sets the final answer, which can be overwritten if another mode is found.
modeFreq = count;
numModes = 1;
} else if (count == maxCount) {
//System.out.println("Mode Counter: " + modeCounter);
int foobar = 0;
while (foobar < data.length) {
if (data[i] == answer[modeCounter]) {
System.out.println("Item is already in modes: " + data[i] + " " + answer[modeCounter]);
break;
} else if (data[i] != answer[modeCounter]){ //This is the if statement that doesn't make sense
answer[modeCounter] = data[i];
System.out.println("idk: " + answer[modeCounter] + " " + data[i]);
if (data[i] == answer[modeCounter]) { //This is the opposite nested if statement, that still runs for some reason
System.out.println("How is this even possible?!?!?");
}
}
foobar++;
}
numModes++;
modeCounter++;
}
i++; //Wouldn't be here if this were a for loop 6 and the Half Blood Prince
}
modes = answer;
}
That was the whole function, but the actual code I'm looking at is here
else if (data[i] != answer[modeCounter]){ //This is the if statement that doesn't make sense
answer[modeCounter] = data[i];
System.out.println("idk: " + answer[modeCounter] + " " + data[i]);
if (data[i] == answer[modeCounter]) { //This is the opposite nested if statement, that still runs for some reason
System.out.println("How is this even possible?!?!?");
}
}
The output in the console is:
Item is already in modes: 43 43
idk: 43 43
How is this even possible?!?!?
Which of course shouldn't be possible, as that calls both if statements.
Thanks in advance for any assistance.
Aucun commentaire:
Enregistrer un commentaire