0

在下图中,为什么totalNotes变量在 Eclipse 中带有警告标志?如果是因为我totalNotes只在 for 循环内部进行了初始化,那么通过该逻辑,costOfSweet变量是否也应该标有警告标志?

代码:导入java.io.BufferedReader;导入 java.io.IOException;导入 java.io.InputStreamReader;导入 java.util.stream.IntStream;

public class BUYING2 {
    static int sum,noOfSweets;
public static void main(String[] args) throws NumberFormatException, IOException {
    BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
    int totalTestCases,costOfSweet,totalNotes;
    int noteDenominations[];
    String firstLineString,secondLineString;
    String secondLineArray[];

    totalTestCases=Integer.parseInt(input.readLine());

    for(int i=0;i<totalTestCases;i++){
        firstLineString=input.readLine();
        secondLineString=input.readLine();
        totalNotes=Integer.parseInt(firstLineString.split(" ")[0]);
        costOfSweet=Integer.parseInt(firstLineString.split(" ")[1]);
        secondLineArray=secondLineString.split(" ");
        noteDenominations=new int[secondLineArray.length];

        for(int k=0;k<secondLineArray.length;k++){
            noteDenominations[k]=Integer.parseInt(secondLineArray[k]);
        }
        System.out.println(getNumberOfSweets(costOfSweet,noteDenominations));
    }
}

public static int getNumberOfSweets(int costOfSweet,int noteDenominations[]) {
    // TODO Auto-generated method stub
    sum=IntStream.of(noteDenominations).sum();
    noOfSweets=sum/costOfSweet;
    int temp;
    for(int j=0;j<noteDenominations.length;j++){
        temp=noteDenominations[j];
        noteDenominations[j]=0;
        if(IntStream.of(noteDenominations).sum()/costOfSweet>=noOfSweets){
            return -1;
        }
        noteDenominations[j]=temp;
    }
    return noOfSweets;
}
}

在此处输入图像描述

4

0 回答 0