繁体   English   中英

Java退出代码:137,未打印任何内容

[英]Java exit code: 137 and nothing printed

我下面的代码应该打印int'numComb'。 但是,事实并非如此。 当我运行它时,直到停止程序,然后出现“退出代码:137”的正确答案,什么都没有发生。 我已经读过137,这意味着JVM可能是一个问题。 但是,我也知道它可能是其他原因导致的,所以我想在代码中知道它的原因,并且如果它与它有关系,则不打印答案。 可能是JVM错误。 谢谢山姆

码:

public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int o = 0;
        int numRem = 0;
        int numLim = 0;
        while((sc.hasNextInt())&&(o<1)) {
            int numT = sc.nextInt();
            numLim = sc.nextInt();
            numRem = sc.nextInt();
            o++;
        }
        List<Integer> persons = new ArrayList<Integer>();
        int nextPer;
        while(sc.hasNextInt()){
            nextPer = sc.nextInt();
            if(nextPer<=numLim) {
                persons.add(nextPer);
            }
        }
        int ns = persons.size();

        int numComb = (factorial(ns)) / ((factorial(numRem)) * (factorial(ns - numRem)));//replace with '1' in reply to comment
        System.out.println(numComb);
        System.exit(0);
    }
    public static int factorial(int n) {
        int f = 1;
        for (int i = 1; i <= n; i++) {
            f *= i;
        }
        return f;
    }

这是整个程序,因为在评论中要求我这样做。 另外,我的测试输入是:

3 2 2 1 2 3

您的错误可能在这里:

    int o = 0;
    int numRem = 0;
    int numLim = 0;
    while((sc.hasNextInt())&&(o<1)) {
  ----> int numT = sc.nextInt();  // You are re-declaring numT
        numLim = sc.nextInt();
        numRem = sc.nextInt();
        o++;
    }

您可以通过在适当的地方添加更多的空格来避免这种编码错误。

例如,我将以这种格式编写您的代码:

int o =         0;
int numRem =    0;
int numLim =    0;

while( sc.hasNextInt() && (o < 1) )
{
    int numT =  sc.nextInt();
    numLim =    sc.nextInt();
    numRem =    sc.nextInt();

    o++;
}

通过遵循编码风格/准则,它不仅使您自己更容易阅读代码,而且使他人也更容易阅读。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM