【问题标题】:Program will only output else statement程序只会输出else语句
【发布时间】:2017-06-05 07:25:53
【问题描述】:

我正在使用 Princeton 的 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++;
        }
    }
}

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

【问题讨论】:

  • 使用调试器找出来
  • @Jens 我该怎么做?抱歉,我对一般编程很陌生
  • 使用谷歌了解如何做到这一点
  • 你用的是什么IDE?
  • @SteveSmith 我正在使用 DrJava

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


【解决方案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

【讨论】:

  • 我不确定你的意思,这不就是我对包含我的 if else 语句的第二个 for 循环所拥有的吗?
  • 当然不是——你的回报在 ELSE,我的在 IF。你的 print -1 在循环内,我的在循环之后。
  • 我明白你现在在说什么了。我按照您的建议做了,但无论我将要查找的整数放在哪里,我仍然得到 -1。你认为使用while循环会更容易吗?诸如 while(array[c] != n) 然后将 if 语句放在后面?
  • 奇怪,它对我来说工作正常......有两个变化:将 -1 部分移到循环之外,添加一个返回到 if 部分。我个人更喜欢 FOR 解决方案,但应该不会有太大不同
  • 我将更新我的原始代码以显示我的循环现在的样子。感谢大家的帮助
【解决方案2】:

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

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

输入尺寸:5

67546

输出将是:

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

代码: 导入 java.util.Scanner; 类 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++;

   }
}
}

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多