Created
November 20, 2017 04:59
-
-
Save s16003/07251c2bb6b11c765a0d71c4e2322d22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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