Home > database >  Why isn't my boolean method printing to console?
Why isn't my boolean method printing to console?

Time:05-29

Could anyone tell me what I am missing here?
I'm just trying to get the sum of the array to display "true" in the console if it is greater than 0.
I've tried all kinds of things already (way too many to list) and I'm stuck. I think it's just something little I might be missing.

When I click on line 27 it says "totalIsHigherThan cannot be resolved to a variable.
I know my curly braces are probably messed up too.
When I print the sum to the console, does it then use that new sum to input into the method? I just want it to list 132 and then true. Thanks for any advice. public static void main(String[] args) {

//9.  Write a method that takes an array of int and returns true if the 
//sum of all the ints in the array is greater than 100.

int[] array = {24, 9, 42, 12, 7, 15, 23};

int sum = 0; 
    
for (int number : array) 
    sum  = number;
    
    System.out.println(sum); 
    

public static boolean totalIsHigherThan (int sum) { 
    
if (sum > 100) {
    return true;
}
    else {return false; {
    
    System.out.println(totalIsHigherThan);
            }
    }

CodePudding user response:

You need to return the result of the boolean equation sum > 0 like that:

public static void main(String[] args) {

  int[] array = {24, 9, 42, 12, 7, 15, 23};
  
  // Call the method
  boolean isGreaterThanHundred = sumGreaterThanHundred(array);

  // Print the result
  System.out.println(isGreaterThanHundred);

}

/*
* Sums up the array and returns true if sum is greater than 100
*/
static boolean sumGreaterThanHundred(int[] arr){
  int sum = 0; 

  for (int number : arr)
      sum  = number;
  
  // return the result as a boolean
  return sum > 100;
}

CodePudding user response:

public class Test {
public static void main(String[] args) {

    int[] array = {24, 9, 42, 12, 7, 15, 23};

    System.out.println(sumGreaterThanHundred(array));

}

private static boolean sumGreaterThanHundred(int[] array) {
    int sum = 0;

    for(int x: array)
    sum =x;

    return sum>100;
}

}

  • Related