简体   繁体   English

程序只会输出else语句

[英]Program will only output else statement

I'm working on creating a linear search in Java using the StdIn library from Princeton, and I can't figure out why my if-else statement will only print out “-1”. 我正在使用普林斯顿大学的StdIn库在Java中创建线性搜索,但无法弄清楚为什么if-else语句仅输出“ -1”。

It seems to me that its skipping over the if block entirely and going straight onto the else . 在我看来,它完全跳过了if块,直接进入了else I'm entering in my list with command-line arguments and ending the list by pressing control-d (not sure what it is on windows, sorry). 我使用命令行参数输入列表,然后按Control-d结束列表(不确定Windows上的内容,对不起)。 Any help at all would be greatly appreciated. 任何帮助将不胜感激。

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

        if (args[0].equals("linear")) {
            int n = Integer.parseInt(args[1]);
            LinearSearch(n);
        }
        else {
            System.out.println("Please enter linear");
        }
   }

   public static void LinearSearch(int n) {
       int x = -1;
       //int u = -1;
       int c, search, array[];
       int value = StdIn.readInt(); //input values
       array = new int[value]; //array list

       while (x < 0) {
           //System.out.println(n);
           //System.out.println("linear");

           //loop to populate array list
           for(c = 0; c < value; c++)
              array[c] = StdIn.readInt();  


           //loop to search for "n"
           for (c = 0; c < value; c++) {
               if(array[c] == n){
                   System.out.println(n + " is at index " + c);
                   x++;
                   return;
               }
               else{
                   continue;
               }
           }

            System.out.println("-1");
            x++;
        }
    }
}

Edit: I updated the entire LinearSearch(n) method. 编辑:我更新了整个LinearSearch(n)方法。 It now finds the value from the list that I enter and gives me the value of -1 when the value is not there. 现在,它从我输入的列表中找到该值,并且当该值不存在时给我值-1 The problem now is that the ArrayList only populates to whatever the first number I enter is when I need to populate to however many int s are entered in the command-line argument 现在的问题是,当我需要填充到命令行参数中输入的许多int时, ArrayList仅填充到我输入的第一个数字。

Your method will return (after printing -1) as soon as the searched value is not in the array: 一旦搜索到的值不在数组中,您的方法将返回(在打印-1之后):

    else{
        System.out.println("-1");
        return;  // EXITING HERE
    }

so if the value entered is not the the first value in the array, you will get -1 and the method is terminated. 因此,如果输入的值不是数组中的第一个值,则将得到-1并且该方法终止。

What you probably want is to return (after printing) as soon as the value IS FOUND, or continue searching up to the last array entry. 您可能想要的是在找到值后立即返回(打印后),或者继续搜索最后一个数组条目。 After this loop exists, that is, nothing was found, you want to print -1 (and return/terminate). 此循环存在后,即未找到任何内容,您想打印-1(并返回/终止)。

Something like 就像是

loop array {
    if value is equal array entry {
        print message
        return
    }
    // else continue looping
}
print -1

Finally after lots of effort I have completed your code. 最后,经过大量的努力,我已经完成了您的代码。

Run program as follows java SearchAlgs 4 5 6 7 如下运行程序java SearchAlgs 4 5 6 7

enter size:5 输入大小:5

67546 67546

Output would be : 输出为:

6 is at index 2 6位于索引2
7 is at index 3 7位于索引3
5 is at index 1 5在索引1
4 is at index 0 4位于索引0

Code: import java.util.Scanner; 代码:import java.util.Scanner; class SearchAlgs { 类SearchAlgs {

    public static void main(String[] args) {
        int list[]=new int[(args.length)-1];
        int conv=0;
        if (args[0].equals("linear")){
            for(int i=0;i<(args.length-1);i++)
            {
                conv=Integer.parseInt(args[i+1]);
                list[i]=conv;
            }
            LinearSearch(list);
        }
        else{
            System.out.println("Please enter linear");
        }
   }


public static void LinearSearch(int n[]){
    int x = -1;
    int c, search, array[];
    Scanner reader = new Scanner(System.in);  // Reading from System.in
    System.out.print("Enter size:");
    int size = reader.nextInt();
    array = new int[size]; //array list

   while (x < 0){
       //loop to populate array list
           for(c = 0; c < size; c++)
              array[c] = reader.nextInt();  

       //loop to search for "n"
       for (c = 0; c < n.length; c++){
            for(int z=0; z<n.length;z++){
               if(array[c] == n[z]){
                   System.out.println(n[z] + " is at index " + z);
                   x++;
               }
               else{
                   continue;
               }
           }
       }
         x++;

   }
}
}

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

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