【问题标题】:The code runs on eclipse but hacker earth ide shows Runtime Error代码在eclipse上运行,但hacker earth ide显示运行时错误
【发布时间】:2017-07-04 17:21:12
【问题描述】:
        import java.util.*;
class TestClass {
      public static void main(String args[] ) throws Exception {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t>0){
        int n=sc.nextInt();
        int pid = sc.nextInt();
        Stack<Integer> st = new Stack<>();
        st.push(pid);
        for(int i=0;i<n;i++){
            String s = sc.next();
            if(s.charAt(0)=='P'){
                st.pop();
                st.push(Integer.parseInt(s.substring(2)));
            }
        }
        int a = st.pop();
        System.out.println("Player "+a);
        t--;
    }
  }
}

我不知道为什么我在这个黑客地球问题中遇到运行时错误,请有人帮忙! 问题链接: https://www.hackerearth.com/practice/data-structures/stacks/basics-of-stacks/practice-problems/algorithm/the-football-fest-6/

【问题讨论】:

  • 您应该以与结构匹配的方式格式化代码。此外,不要声明不必要的static 变量。当你有一个计数变量时,使用for 循环。并且不要通过不同的对象阅读System.in。始终使用Scanner。不需要额外的BufferedReader
  • 感谢您的回复,我已经更改了代码,然后我一直在使用扫描仪,这个在 eclipse 中显示正确的输出,但在 Hackerearth 上再次失败!它仍然显示运行时错误。
  • 您正在处理 'P' 事件,而不是 'B' 事件。你最后只弹出一次。您应该使用不同的输入进行测试。
  • Hacker earth java 编辑器对用户不友好,不要在其中浪费时间。Edior 应该像 eclipse 编辑器,IntelJi 编辑器一样友好。他们不知道如何实现友好的编辑器,只是困扰所有候选人

标签: data-structures java-8 stack


【解决方案1】:

我自己不是java coder,但据我所知,问题出在

Scanner sc = new Scanner(System.in);

int t = sc.nextInt();

我认为这意味着您正在寻找同一行中的输入。 Hacker earth 不会在同一行中提供所有输入。大多数输入由新行终止。第一行输入总是测试用例的数量(大部分)。我认为这件事应该会有所帮助。

sc.nextLine();   

确保您在 Eclipse 中向程序提供输入的方式与在黑客地球中接收的方式相同。

【讨论】:

    猜你喜欢
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    相关资源
    最近更新 更多