如何查找数字是否在数组中?

use*_*817 1 java methods for-loop if-statement boolean

试着写一个方法告诉我数字是否在数组中.到目前为止我有..

    public static boolean inList(double number, double[] list){
    for (double i : list)
        {
            if (i == number){
                return false;
        }

    }
    return true; //if its not in the array I want the if statement to run.
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,这对我没有用,由于某种原因它无法识别阵列中的某些数字是否存在.

But*_*ass 6

你混淆了你的return陈述.它应该是这样的:

    public static boolean inList(double number, double[] list){
        for (double i : list)
        {
            if (i == number){
                //return true when you've found the match!
                return true;
            }
        }
    //return false here because you finished looking at the entire array 
    //and couldn't find a match.
    return false;
}
Run Code Online (Sandbox Code Playgroud)