返回false在Java中不起作用

时间:2017-11-20 03:54:45

标签: java eclipse return

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] numList = {983, 235, 92, 118, 79, 203, 199};
    boolean found;
    System.out.println("Enter the key: ");
    int key = input.nextInt();

    System.out.println("\nThe arrays: ");
    printArray(numList);

    if(found = searchKey(numList)) {
        System.out.println(key + " is found in the array");
    } else {
        System.out.println(key + " is not found in the array");
    }
    input.close();
}

public static boolean searchKey(int[] numList) {
    int key = numList[1];
    for (int i = 1; i < numList.length; i++);
    if(key == numList[1])
        return true;
    else
        return false;
}

public static void printArray(int[] numList) {
    for (int value : numList)
        System.out.println(value + "\t");
    System.out.println();
}

这个程序的输出只显示在数组中找到的,即使我输入一个无效的整数(例如:4),当结果应为&#34; 4时未找到阵列&#34 ;.我错过了什么?

6 个答案:

答案 0 :(得分:1)

for (int i = 1; i < numList.length; i++);

我认为;在行尾是可疑的。您可能想要删除它。

答案 1 :(得分:1)

此代码应该适合您。我已经为函数添加了key类型的参数int,这将是用户将要输入的数字。我在for loop中附上了搜索内容,并将if表达式更改为key == numList[i]。这里将密钥与数组列表中的每个元素进行比较。

请记住,如果在数组列表中找到匹配的元素,则只返回true。如果for循环退出而没有在数组列表中找到匹配的元素,它将返回false

public static boolean searchKey(int[] numList, int key) {
    for (int i = 0; i < numList.length; i++){
        if(key == numList[i])
            return true;
    }
    return false;
}

由于我在searchKey()方法中添加了一个新参数,因此也必须修改对该方法的调用。所以我将传递数组列表以及用户输入的密钥,如下所示。

if(found = searchKey(numList, key)) {
    System.out.println(key + " is found in the array");
} else {
    System.out.println(key + " is not found in the array");
}

答案 2 :(得分:1)

我认为您正在尝试实施线性搜索。您必须在搜索方法中添加另一个参数&#34; key&#34;

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] numList = {983, 235, 92, 118, 79, 203, 199};

    System.out.println("Enter the key: ");
    int key = input.nextInt();

    System.out.println("\nThe arrays: ");
    printArray(numList);

    if(searchKey(numList, key)) 
        System.out.println(key + " is found in the array");
     else 
        System.out.println(key + " is not found in the array");

    input.close();
}

public static boolean searchKey(int[] numList, int key) {

    for (int i = 0; i < numList.length; i++) {
        if(key == numList[i])
            return true; // If anywhere key is found, it will return true
    }
    return false;  // After traversing whole list if key is not found, then it will return false
}

public static void printArray(int[] numList) {
    for (int value : numList)
        System.out.print(value + "\t");
    System.out.println();
}

答案 3 :(得分:0)

numList[1]不是关键,

该功能应该是:

public static boolean searchKey(int[] numList, int key) {
    boolean status = false;
    for (int i = 0; i < numList.length; i++){
        if(key == numList[i])
            status = true;
    }
    return status;
}

并称之为:

if(searchKey(numList, key)) {
    System.out.println(key + " is found in the array");
} else {
    System.out.println(key + " is not found in the array");
}

答案 4 :(得分:0)

如果我们以正确的方式编写它,返回false总是在java中工作:)

为了实现所需的功能,很少需要进行修改。

  1. 密钥未传递给searchKey()以执行实际搜索
  2. for循环用;最终没有用来实现所需的功能,因此已被删除。
  3. 数组索引从第0位开始。
  4. 通过上述调整确定了工作计划。

    如果您愿意,可以进一步优化。

    public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int[] numList = {983, 235, 92, 118, 79, 203, 199};
            boolean found;
           System.out.println("Enter the key: ");
        int key = input.nextInt();
    
        System.out.println("\nThe arrays: ");
        printArray(numList);
    
        if(searchKey(numList, key)) {
            System.out.println(key + " is found in the array");
        } else {
            System.out.println(key + " is not found in the array");
        }
        input.close();
    }
    
    public static boolean searchKey(int[] numList, int key) {
        for (int i = 0; i < numList.length; i++) {
            if (key == numList[i])
                return true;
        }
        return false;
    }
    
    public static void printArray(int[] numList) {
        for (int value : numList)
            System.out.println(value + "\t");
        System.out.println();
    }    
    

答案 5 :(得分:0)

您的函数searchKey(int [] numList)写错了。您需要将密钥与数组一起传递。您没有传递任何键并将数组的第一个元素作为键并将数组的第一个元素与键进行比较(这只是数组的第一个元素)。

您的代码:

public static boolean searchKey(int[] numList) {
int key = numList[1];
for (int i = 1; i < numList.length; i++);
if(key == numList[1])
    return true;
else
    return false;
}

应该是:

public static boolean searchKey(int key, int[] numList) {
    for (int i = 0; i < numList.length; i++) {
        if(key == numList[i])
            return true ;
    }
    return false ;
}