Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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
Java 程序将只输出else语句_Java_Arrays_If Statement_While Loop_Stdin - Fatal编程技术网

Java 程序将只输出else语句

Java 程序将只输出else语句,java,arrays,if-statement,while-loop,stdin,Java,Arrays,If Statement,While Loop,Stdin,我正在使用普林斯顿的StdIn库在Java中创建一个线性搜索,我不明白为什么我的if-else语句只输出“-1” 在我看来,它完全跳过了if块,直接进入else。我正在使用命令行参数输入列表,并通过按control-d结束列表(抱歉,不确定它在windows上是什么)。任何帮助都将不胜感激 public class SearchAlgs { public static void main(String[] args) { if (args[0].equals("line

我正在使用普林斯顿的StdIn库在Java中创建一个线性搜索,我不明白为什么我的
if-else
语句只输出“-1”

在我看来,它完全跳过了
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++;
        }
    }
}
公共类搜索{
公共静态void main(字符串[]args){
if(args[0]。等于(“线性”)){
int n=Integer.parseInt(args[1]);
线性搜索(n);
}
否则{
System.out.println(“请输入线性”);
}
}
公共静态无效线性搜索(int n){
int x=-1;
//int u=-1;
int c,搜索,数组[];
int value=StdIn.readInt();//输入值
array=newint[value];//数组列表
而(x<0){
//系统输出println(n);
//System.out.println(“线性”);
//循环以填充数组列表
对于(c=0;c
编辑: 我更新了整个
LinearSearch(n)
方法。现在,它从我输入的列表中查找值,并在值不存在时为我提供值
-1
。现在的问题是,
ArrayList
只会填充到我输入的第一个数字,当我需要填充到命令行参数中输入的
int
s的数量时,

只要搜索的值不在数组中,您的方法就会返回(打印后为-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

经过大量的努力,我终于完成了你的代码

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

输入大小:5

67546

产出将是:

6位于索引2
7位于索引3
5位于索引1
4位于索引0处

代码: 导入java.util.Scanner; 类搜索{

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

   }
}
}
publicstaticvoidmain(字符串[]args){
int list[]=新int[(参数长度)-1];
int conv=0;
if(args[0]。等于(“线性”)){

对于(int i=0;i使用调试器查找它out@Jens我该怎么做呢?对不起,我对使用google进行编程很陌生,我不知道该怎么做。你用的是什么IDE?@SteveSmith我用的是DrJavaI我不知道你的意思,这不就是我在第二个for循环中使用的,它包含if-else语句吗?当然不是-你的返回值我在ELSE中,我的在IF中。你的print-1在循环中,我的在循环后。我明白你的意思了。我按照你的建议做了,但是我仍然得到-1,不管我把我要找的整数放在哪里。你认为使用while循环更容易吗?比如while(数组[c]!=n)然后把if语句放在后面?奇怪,它对我来说工作得很好…有两种变化:将-1部分移到循环之外,向if部分添加一个返回。我个人更喜欢for解决方案,但应该与a没有太大区别。我将更新我的原始代码,以显示我的循环现在的样子。感谢所有的帮助