Skip to content

Instantly share code, notes, and snippets.

@s16003
Created November 20, 2017 04:59
Show Gist options
  • Select an option

  • Save s16003/07251c2bb6b11c765a0d71c4e2322d22 to your computer and use it in GitHub Desktop.

Select an option

Save s16003/07251c2bb6b11c765a0d71c4e2322d22 to your computer and use it in GitHub Desktop.
import java.util.*;
class testScoreAverage {
public static void main(String[] args) {
ScoreAverage scoreAverage = new ScoreAverage();
Scanner sc = new Scanner(System.in);
int n;
while (true) {
System.out.print("値を入力してください: ");
n = sc.nextInt();
if (n < 0 || 100 < n) {
break;
}
scoreAverage.add(n);
}
n = scoreAverage.ave();
System.out.printf(n == -1 ? "入力はありません。\n" : "平均は%d\n", n);
}
}
class ScoreAverage {
List<Integer> list = new ArrayList<>();
private int sum;
public void add(int n) {
list.add(n);
}
public int ave() {
if (list.isEmpty()) {
return -1;
}
for (int a : list) {
sum += a;
}
return sum / list.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment