【问题标题】:Switch statement ends prematurely in for loopswitch 语句在 for 循环中过早结束
【发布时间】:2017-03-30 03:16:22
【问题描述】:

所以我正在做一个 HackerRank 问题,但由于某种原因似乎无法通过所有测试。我的代码通过了第一次运行,但似乎没有通过所有其他测试。通过调试,似乎 for 循环在使用完所有 switch case 后终止。

这个练习是一个 Java 列表问题。给出一个输入,例如

5
12 0 1 78 12
2
Insert
5 23
Delete
0

第一行包含一个整数,N(列表中元素的初始数量,在本例中为 5)。 第二行包含组成列表的 N 个空格分隔的整数。 第三行包含一个整数,q(查询数)。 随后的几行描述了查询,每个查询用两行来描述:

如果查询的第一行包含字符串Insert,那么第二行包含两个空格分隔的整数,并且该值必须插入到索引处。 如果查询的第一行包含字符串 Delete,则第二行包含 index ,其元素必须从 中删除。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class ListSolution {
public static void main(String[] args){
    List<Integer> list1 = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    sc.nextLine();
    for(int i =0;i<n;i++){
        list1.add(sc.nextInt());
    }

    sc.nextLine();
    int q = sc.nextInt();
    sc.nextLine();
    for(int i=0;i<q;i++){
        switch(sc.nextLine()){
        case "Insert" :
            int x = sc.nextInt();
            int y = sc.nextInt();
            sc.nextLine();
            list1.add(x,y);             
            continue;
        case "Delete" :
            int z = sc.nextInt();
            list1.remove(z);
            continue;
        default:
            continue;
        }
    }
    for(int i=0;i<list1.size(); i++){
        System.out.print(list1.get(i)+" ");;
    }
}
}

我能够完成问题并传递这个特定的输入。但是,它失败了所有其他,其中有 3 个查询,其中插入和删除是前两个。似乎一旦切换完成所有情况,它就会终止 for 循环,即使还有更多的迭代要做。这种输入的一个例子是:

10
100 50 20 10 70 80 200 259 1 900
3
Insert
2 80
Delete
6
Insert
5 89

【问题讨论】:

  • 你想要的输出是什么?你的问题是什么?
  • 如果您在 Delete 案例中包含 sc.nextLine() 会发生什么情况,就像您在 Insert 案例中所做的那样?
  • 它会抛出异常。读取最后一个数字后,输入没有下一行。除了我在下面的回答之外,他还可以设置一个 if 条件,例如 if sc.hasNextLine() 然后 sc.nextLine()。最初,在第一次删除查询之后没有以正确的方式读取输入。

标签: java for-loop switch-statement


【解决方案1】:

在 switch 情况下,使用 sc.next() 而不是 sc.nextLine()。最后,从插入案例中删除 sc.nextLine()。

【讨论】:

  • 成功了!你能解释一下这两个编辑是如何解决所有问题的吗?我做 nextLine 的原因是因为我认为扫描仪会在之后读取一个空格,而不是向下一行。仅当一条线上有两个以上的东西时才适用?换句话说,如果我们只读取一行中的一项内容,那么就不需要 nextLine 了,因为那一项之后没有空格?
【解决方案2】:

这是一个可行的解决方案:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class ListSolution {
    public static void main(String[] args){
        List<Integer> list1 = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        for(int i =0;i<n;i++){
            list1.add(sc.nextInt());
        }

        sc.nextLine();
        int q = sc.nextInt();
        sc.nextLine();
        for(int i=0;i<=q;i++){
            String input = sc.nextLine();
            switch(input) {
                case "Insert" :
                    int x = sc.nextInt();
                    int y = sc.nextInt();
                    sc.nextLine();
                    list1.add(x,y);
                    continue;
                case "Delete" :
                    int z = sc.nextInt();
                    list1.remove(z);
                    sc.nextLine();
                    continue;
                default:
                    continue;
            }
        }
        for(int i=0;i<list1.size(); i++){
            System.out.print(list1.get(i)+" ");;
        }
    }
}

您忘记的主要事情是删除后您没有请求获取新行。所以在你的第二个例子中,开关是在一个空行返回,因此你得到了错误的值。

如果我在做这个练习,我会将每一行作为一个字符串,然后解析它。例如。获取整数:

String[] numbers = sc.nextLine().split(" ");
for(String number: numbers){
    list1.add(Integer.valueOf(number));
}

这样你就不会有奇怪的行尾混淆。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多