Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Return false在Java中不起作用 publicstaticvoidmain(字符串[]args){ 扫描仪输入=新扫描仪(System.in); int[]numList={983,235,92,118,79,203,199}; 布尔发现; System.out.println(“输入键:”); int key=input.nextInt(); System.out.println(“\n数组:”); 打印阵列(numList); if(found=searchKey(numList)){ System.out.println(在数组中找到键+); }否则{ System.out.println(在数组中找不到键+); } input.close(); } 公共静态布尔搜索键(int[]numList){ int key=numList[1]; for(int i=1;i_Java_Eclipse_Return - Fatal编程技术网

Return false在Java中不起作用 publicstaticvoidmain(字符串[]args){ 扫描仪输入=新扫描仪(System.in); int[]numList={983,235,92,118,79,203,199}; 布尔发现; System.out.println(“输入键:”); int key=input.nextInt(); System.out.println(“\n数组:”); 打印阵列(numList); if(found=searchKey(numList)){ System.out.println(在数组中找到键+); }否则{ System.out.println(在数组中找不到键+); } input.close(); } 公共静态布尔搜索键(int[]numList){ int key=numList[1]; for(int i=1;i

Return false在Java中不起作用 publicstaticvoidmain(字符串[]args){ 扫描仪输入=新扫描仪(System.in); int[]numList={983,235,92,118,79,203,199}; 布尔发现; System.out.println(“输入键:”); int key=input.nextInt(); System.out.println(“\n数组:”); 打印阵列(numList); if(found=searchKey(numList)){ System.out.println(在数组中找到键+); }否则{ System.out.println(在数组中找不到键+); } input.close(); } 公共静态布尔搜索键(int[]numList){ int key=numList[1]; for(int i=1;i,java,eclipse,return,Java,Eclipse,Return,该程序的输出仅显示数组中的find,即使我键入了无效整数(例如:4),结果应为“数组中未找到4”。我遗漏了什么?for(inti=1;I

该程序的输出仅显示数组中的find,即使我键入了无效整数(例如:4),结果应为“数组中未找到4”。我遗漏了什么?

for(inti=1;Ipublic 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();
}

我认为,;这条线的尽头是可疑的。您可能想删除它

numList[1]
不是键

该功能应为:

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

此代码应该适用于您。我已将类型为
int
的参数
key
添加到函数中,这将是用户要输入的数字。我已将搜索包含在
for循环中
,并将
if
表达式更改为
key==numList[I]
。这里我将把键与数组列表中的每个元素进行比较

记住,如果我在数组列表中找到匹配的元素,我只返回
true
。如果for循环退出时未在数组列表中找到匹配元素,它将返回
false

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

我认为您正在尝试实现线性搜索。您必须在搜索方法“key”中添加另一个参数

publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int[]numList={983,235,92,118,79,203,199};
System.out.println(“输入键:”);
int key=input.nextInt();
System.out.println(“\n数组:”);
打印阵列(numList);
if(searchKey(numList,key))
System.out.println(在数组中找到键+);
其他的
System.out.println(在数组中找不到键+);
input.close();
}
公共静态布尔搜索键(int[]numList,int key){
for(int i=0;i
如果我们以正确的方式编写,Return false在java中始终有效:)

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

  • 未将密钥传递给searchKey()以执行实际搜索
  • 对于带有的循环;最后是没有用的,以实现所需的功能,因此已被删除
  • 数组索引从第0个位置开始
  • 已经用以上的调整放下了工作程序

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

    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();
    }
    
    publicstaticvoidmain(字符串[]args){
    扫描仪输入=新扫描仪(System.in);
    int[]numList={983,235,92,118,79,203,199};
    布尔发现;
    System.out.println(“输入键:”);
    int key=input.nextInt();
    System.out.println(“\n数组:”);
    打印阵列(numList);
    if(searchKey(numList,key)){
    System.out.println(在数组中找到键+);
    }否则{
    System.out.println(在数组中找不到键+);
    }
    input.close();
    }
    公共静态布尔搜索键(int[]numList,int key){
    for(int i=0;i
    您的函数搜索键(int[]numList)写入错误。您需要将密钥与数组一起传递。您没有传递任何键并将数组的第一个元素作为键,并将数组的第一个元素与键(这只是数组的第一个元素)进行比较

    您的代码:

    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();
    }    
    
    公共静态布尔搜索键(int[]numList){
    int key=numList[1];
    for(int i=1;i
    应该是:

    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;
    }
    
    公共静态布尔搜索键(int-key,int[]numList){
    for(int i=0;i
    for(int i=1;i即使您删除;在for循环之后,但键将始终等于numList[1],并在第一次迭代本身时返回true
    int key=numList[1];if(key==numList[1])
    将始终为真。我按指示执行了操作,在我的搜索键(int[]numList)中形成了一条波浪形红线,我按指示执行了操作,出现了一个新错误;我的searchKey(int[]numList,int key)上形成了一条波浪形的红线@fatind:我忘了
    在状态结束时,再次尝试并删除它。现在只有返回错误的工作。每当我键入正确的整数时,我仍然会得到一个“(插入有效整数)在数组中找不到”这样做。现在只有返回错误的工作。每当我键入正确的整数时,我仍然会得到一个“(插入有效整数)在数组中找不到”运行此程序,返回true不起作用。@fatind请参阅。它起作用了
    
    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();
    }
    
    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();
    }    
    
    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 ;
    }