This is a part of my 2nd method.. the method is using the first and last values of an array given by my main method and adding them together. If the sum is above (in my example "20"), then j
(the last value of the array) decreases. If the sum is below 20, i
(first value of the array) is increased. The process is repeated until either i == j
or i + j = 20
I am using a do ... while loop with a couple of if statements...but for some reason, my program keeps running and gives no result. Help?
public static int checkSum(int[] array){
int i = 0;
int j = 6;
int sum;
sum = array[i] + array[j];
do {
if (sum == 20) {
break;
}
else if (sum < 20) {
i = i++;
sum = array[i] + array[j];
}
else {
j = j--;
sum = array[i] + array[j];
System.out.print("No");
}
sum = array[i] + array[j];
} while (i != j);
if (i == j) {
i = -1;
}
return i;
For the sake of not spamming the website.. i
should be 2... but my program gives no results and keeps running infinitely..