程序只会输出else语句

时间:2017-01-20 10:30:11

标签: java arrays if-statement while-loop stdin

我正在使用普林斯顿的StdIn库在Java中创建线性搜索,我无法弄清楚为什么我的if-else语句只打印出“-1”。< / p>

在我看来,它完全跳过if区块并直接进入else。我用命令行参数进入我的列表并按control-d结束列表(不知道它在windows上是什么,对不起)。任何帮助都将非常感激。

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

修改 我更新了整个LinearSearch(n)方法。它现在从我输入的列表中找到值,并在值不存在时给我-1的值。现在的问题是ArrayList只填充我输入的第一个数字,当我需要填充到命令行参数中输入的许多int

2 个答案:

答案 0 :(得分:1)

一旦搜索到的值不在数组中,您的方法将返回(在打印-1之后):

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

因此,如果输入的值不是数组中的第一个值,则将获得-1并终止该方法。

您可能想要的是在值找到后立即返回(打印后),或继续搜索到最后一个数组条目。在此循环存在之后,即没有找到任何内容,您要打印-1(并返回/终止)。

这样的东西
loop array {
    if value is equal array entry {
        print message
        return
    }
    // else continue looping
}
print -1

答案 1 :(得分:0)

经过多次努力,我完成了你的代码。

运行程序如下java SearchAlgs 4 5 6 7

  

输入尺寸:5

     

67546

输出将是:

  

6在索引2处   7是索引3
  5是索引1   4是在索引0

代码:     import java.util.Scanner;     class 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++;

   }
}
}