【发布时间】:2016-07-20 00:29:05
【问题描述】:
我正在尝试制作一个程序,打印出您输入的三个数字,然后输出这些数字的平均值。创建所需方法后,当我编译代码时,使用带有scores 的方法时出现错误。我不确定在调用mPrint() 或average() 时如何引用scores。
编译代码时,会在scores.mPrint(3); 和scores.average(); 所在的行抛出错误。这些错误是:
The method(方法在这里)is undefined for the type ArrayList<Double>.
我导入了:java.util.ArrayList 和 java.util.Scanner
public class OOPtraining {
ArrayList<Double> scores = new ArrayList<Double>();
public void mPrint(Integer prints) {
for (Integer i =0;i<prints; i++) {
System.out.println(scores.get(i));
}
}
public void average() {
double divi = scores.get(0)+scores.get(1)+scores.get(2);
System.out.println(divi/3);
}
public void main(String[] args) {
ArrayList<Double> scores = new ArrayList<Double>();
Scanner reader = new Scanner(System.in);
// down here is where I input the scores and then add them to "scores"
System.out.println("Enter a score: ");
double score1 = reader.nextDouble();
while (true) {
System.out.println("Type a double-type number:");
try {
score1 = Double.parseDouble(reader.next());
break;
} catch (NumberFormatException ignore) {
System.out.println("Invalid input");
}
}
scores.add(score1);
System.out.println("Enter another score: ");
double score2 = reader.nextDouble();
while (true) {
System.out.println("Type a double-type number:");
try {
score2 = Double.parseDouble(reader.next());
break;
} catch (NumberFormatException ignore) {
System.out.println("Invalid input");
}
}
scores.add(score2);
System.out.println("Enter another score: ");
double score3 = reader.nextDouble();
while (true) {
System.out.println("Type a double-type number:");
try {
score3 = Double.parseDouble(reader.next());
break;
} catch (NumberFormatException ignore) {
System.out.println("Invalid input");
}
}
scores.add(score3);
//here is where I stop adding the scores to "scores"
scores.mPrint(3);
scores.average();
}
}
【问题讨论】:
-
get(...) 中有什么?
-
在哪方面看起来不正确?
-
请告诉我们您希望这样做。这样做毫无意义。
-
你的问题很不清楚,请更明确一点
-
对不起,上面的问题很神秘,但我想知道的是如何引用 ArrayList,如果它被调用(在这种情况下,
scores)当我创建一个方法时。
标签: java oop arraylist methods this