为什么整数变量在Eclipse中显示警告标志?

时间:2016-11-03 20:22:36

标签: java eclipse for-loop variable-initialization

在下图中,为什么totalNotes变量在Eclipse中显示警告标志?如果是因为我在for循环中初始化了totalNotes,那么那个逻辑不应该costOfSweet变量也标有警告标志吗?

代码:     import java.io.BufferedReader;     import java.io.IOException;     import java.io.InputStreamReader;     import 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;
}
}

enter image description here

0 个答案:

没有答案