CodeChef程序(JAVA)中的运行时错误(非零退出代码)

时间:2017-12-05 13:16:45

标签: java runtime-error bluej

这是问题 - https://www.codechef.com/DEC17/problems/CPLAY

这是我的解决方案 -

import java.util.Scanner;

class Penalty_Shoot_Out
{

    public static void main(String args[]) 
    {
        Scanner sc = new Scanner(System.in);
        //System.out.println("T: ");
        int T = sc.nextInt();
        sc.nextLine();
        while(T-->0)
        {
            String s = sc.nextLine();
            int l = s.length();
            s = " " + s;
            int A_score = 0, B_score = 0, A_shots = 0, B_shots = 0, flag = 0, A_shots_left = 5, B_shots_left = 5, shots_left = 0;
            Outer:for(int i=1; i<=20; i++)
            {
                char c = s.charAt(i);
                if(i%2==1)
                {
                    flag = 0;
                    A_shots++;
                    A_shots_left--;
                    if(c=='1')
                        A_score++;
                }
                else
                {
                    flag = 1;
                    B_shots++;
                    B_shots_left--;
                    if(c=='1')
                        B_score++;
                }
                if(i<=10)
                {
                    if(A_score<B_score)
                    {
                        if(B_score-A_score>A_shots_left)
                        {
                            System.out.println("TEAM-B " + i);
                            break Outer;
                        }
                    }
                    else
                    {
                        if(A_score-B_score>B_shots_left)
                        {
                           System.out.println("TEAM-A " + i);
                           break Outer;
                        }
                    }
                }
                else if(i>10 && i<=20)
                {
                    if(i%2==0)
                    {
                        if(A_score>B_score)
                            System.out.println("TEAM-A " + i);
                        else if(B_score>A_score) 
                            System.out.println("TEAM-B " + i);
                        else
                            System.out.println("TIE");
                        break Outer;
                    }
                }
            }
        }
    }
}        

这些是例外 -

例外

in thread "main" java.util.NoSuchElementException

    at java.util.Scanner.throwFor(Scanner.java:862)

    at java.util.Scanner.next(Scanner.java:1485)

    at java.util.Scanner.nextInt(Scanner.java:2117)

    at java.util.Scanner.nextInt(Scanner.java:2076)

    at Penalty_Shoot_Out.main(Main.java:8)

我在计算机上得到了正确答案,但是当我在线提交时,我得到 NZEC Runtime 错误。
我尝试使用谷歌搜索解决方案,大多数人说错误可能与主函数返回错误的数字或不返回数字有关。有人说这可能是由于使用特定功能或由于我在I / O期间没有处理的异常。但是,我无法弄明白。

如果有人可以修复我的代码,我真的很感激。

1 个答案:

答案 0 :(得分:0)

你应该把你的解决方案放在试试中。

public static void main(String args[]) 
{
  try {
    Scanner sc = new Scanner(System.in);
    //System.out.println("T: ");
    int T = sc.nextInt();
    sc.nextLine();
    while(T-->0)
    {
       String s = sc.nextLine();
        int l = s.length();
        s = " " + s;
        int A_score = 0, B_score = 0, A_shots = 0, B_shots = 0, flag = 0,
            A_shots_left = 5, B_shots_left = 5, shots_left = 0;
        Outer:for(int i=1; i<=20; i++)
        {
            char c = s.charAt(i);
            if(i%2==1)
            {
                flag = 0;
                A_shots++;
                A_shots_left--;
                if(c=='1')
                    A_score++;
            }
            else
            {
                flag = 1;
                B_shots++;
                B_shots_left--;
                if(c=='1')
                    B_score++;
            }
            if(i<=10)
            {
                if(A_score<B_score)
                {
                    if(B_score-A_score>A_shots_left)
                    {
                        System.out.println("TEAM-B " + i);
                        break Outer;
                    }
                }
                else
                {
                    if(A_score-B_score>B_shots_left)
                    {
                       System.out.println("TEAM-A " + i);
                       break Outer;
                    }
                }
            }
            else if(i>10 && i<=20)
            {
                if(i%2==0)
                {
                    if(A_score>B_score)
                        System.out.println("TEAM-A " + i);
                    else if(B_score>A_score) 
                        System.out.println("TEAM-B " + i);
                    else
                        System.out.println("TIE");
                    break Outer;
                }
            }
        }
    }
  } catch(Exception e) {
    return;
  }
}

}