所以我已经研究这个问题有一段时间了。我一直在找工作 ArrayIndexOutOfBoundsException
但我无法找到问题所在。如果有人能给我指出正确的方向,我会非常感激的!谢谢!
public class Answer {
public static void main(String[] args){
double[] y = {23, 11.1, 50.4};
double[] x = {22.2, 46, 100.0};
Answer answer = new Answer();
answer.answer(y, x);
}
public static int answer(double[] y, double[] x) {
int result = 0;
double percent_1, percent_2;
double[] compareList_1 = new double[x.length];
double[] compareList_2 = new double[y.length];
// Calculate percent of first 2 x value array items with y
// all y values. Store the results in a seperate list.
for(int i = 0; i < x.length; i++){
percent_1 = compare(y[i], x[0]);
percent_2 = compare(y[i], x[1]);
compareList_1[i] = percent_1;
compareList_2[i] = percent_2;
}
// Compare those lists to find common number
// There you have your answer.
result = (int)compareLists(compareList_1, compareList_2);
return result;
}
// Calculates percentage from x and y values
public static double compare(double y, double x){
double result = 1 - (y/x);
return result;
}
// Finds common value in lists
public static double compareLists(double[] list_1, double[] list_2){
for(int i = 0; i < list_1.length + 1; i++){
for(int j = 0; j < list_2.length + 1; j++){
if(list_1[i] == list_2[j]){
return list_1[i];
}
}
}
// Just cus this shouldn't ever return.
return 100;
}
}
2条答案
按热度按时间pbpqsu0x1#
在迭代(comparelists)中,应该使用“length”(而不是length+1)
dsf9zpds2#
我想问题出在
for(int i = 0; i < list_1.length; i++){
for(int j = 0; j < list_2.length ; j++){
double[] compareList_1 = new double[x.length];
double[] compareList_2 = new double[y.length];