Java 代码编译并执行,但不打印任何内容

Java 代码编译并执行,但不打印任何内容,java,Java,延长的时间限制是执行以下代码的成功编译类文件时的状态 import java.io.*; public class CandidateCode { public static int ThirstyCrowProblem(int[] input1, int input2, int input3) { int[] arrK = new int[input3]; int minstones = 0; for (int i = 0; i <

延长的时间限制是执行以下代码的成功编译类文件时的状态

import java.io.*;
public class CandidateCode {

    public static int ThirstyCrowProblem(int[] input1, int input2, int input3) {
        int[] arrK = new int[input3];
        int minstones = 0;
        for (int i = 0; i < input3; i++) //create an array of k Os.
        {
            int smallest = input1[0], place = 0;
            for (int j = 0; j < input2; j++) {
                if ((smallest >= input1[j]) && (input1[j] >= 0)) {
                    smallest = input1[j];
                    place = j;
                }
            }
            input1[place] = -1;
            arrK[i] = smallest;
        }
        int n = input2, i = 0;
        while (i < input3)
        minstones = minstones + arrK[i] * (n - i);
        return minstones;
    }

    public static void main(String[] args) {
        int[] arr = new int[] {
            5, 58
        };
        int stones_min = CandidateCode.ThirstyCrowProblem(arr, 2, 1);
        System.out.println("The result is" + stones_min);
    }
}
import java.io.*;
公共类候选代码{
公共静态整数排序问题(int[]input1,int input2,int input3){
int[]arrK=新的int[input3];
int minstones=0;
for(inti=0;i=input1[j])&&(input1[j]>=0)){
最小值=输入1[j];
地点=j;
}
}
输入1[位置]=-1;
arrK[i]=最小值;
}
int n=input2,i=0;
而(i

光标一直在等待,但我认为代码中没有错误

选项A: 将while更改为if语句:

if(i<input3) {
    minstones= minstones + arrK[i]*(n-i);
}

if(i您需要在while循环中增加
i
。因为您不是在增加,所以它将进入无限循环

while(i<input3)
{
    minstones= minstones + arrK[i]*(n-i);
    i++;
}

您的代码中有两个循环。猜猜哪一个是无限的……呃。这纯粹是风格的问题,但在循环内容周围使用括号,即使只有一条语句。@zubergu,谢谢,但如果不是下面的注释,我不会理解。
while(i<input3)
{
    minstones= minstones + arrK[i]*(n-i);
    i++;
}
The result is10