在下图中,为什么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;
}
}