私はあなたが入力した3つの数字を出力し、それらの数字の平均を出力するプログラムを作ろうとしています。必要なメソッドを作成した後、コードをコンパイルすると、scores
のメソッドを使用するとエラーが発生しました。 scores
をmPrint()
またはaverage()
と呼ぶときに、どのように参照することができないのか分かりません。呼び出すオブジェクトを参照するメソッドを作成するにはどうすればよいですか?
コードをコンパイルすると、scores.mPrint(3);
とscores.average();
の行にエラーがスローされます。これらのエラーは、 The method
(ここの方法)is undefined for the type ArrayList<Double>
です。
私は、インポート:あなたが宣言したクラスがgetメソッドを持っていないため、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();
}
}
何が得られますか(...)? –
どのようにして間違っているようですか? –
あなたの期待することを教えてください。それは意味をなさない。 –