简体   繁体   English

为什么我的布尔方法没有打印到控制台?

[英]Why isn't my boolean method printing to console?

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.我只是想让数组的总和在控制台中显示“true”,如果它大于 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.当我单击第 27 行时,它显示“totalIsHigherThan 无法解析为变量。
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.我只希望它列出 132 然后是真的。 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);
            }
    }

You need to return the result of the boolean equation sum > 0 like that:您需要像这样返回布尔方程sum > 0的结果:

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;
}
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;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM