ある掲示板でこんな質問があった。
学校で出された課題です。自分でいろいろやってみたのですが上手くいきません。条件分岐の問題です。 プログラム作成内容 A,B,Cの3人の学生のテストの点数を入力して、 ・3人の点数の合計 ・3人の点数の平均点 ・3人の中の最高点 ・3人の中の最低点 を出力できるプログラム
指定された言語がJavaだったんでこんなん組んでみた。
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Kadai { public static void main(String[] args) { List<Student> students = initializeStudents(); inputScores(students); Collections.sort(students); int sum = calcTotalScore(students); double average = (double) sum / students.size(); int max = students.get(students.size() - 1).getScore(); int min = students.get(0).getScore(); System.out.println("三人の合計得点: " + sum); System.out.println("三人の点数の平均点: " + average); System.out.println("三人の中の最高点: " + max); System.out.println("三人の中の最低点: " + min); } private static int calcTotalScore(List<Student> students) { int sum = 0; for (Student student : students) { sum += student.getScore(); } return sum; } private static List<Student> initializeStudents() { List<Student> students = new ArrayList<Student>(3); for (Student student : new Student[]{new Student("A"), new Student("B"), new Student("C")}) { students.add(student); } return students; } private static void inputScores(List<Student> students) { Scanner scanner = new Scanner(System.in); for (int i = 0; i < students.size(); i++) { Student student = students.get(i); System.out.print(student.getName() + ": "); student.setScore(scanner.nextInt()); } } } class Student implements Comparable<Student> { private String name; private int score; public Student(String name) { if (name == null || "".equals(name)) { throw new IllegalArgumentException(); } this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int compareTo(Student other) { if (other == null) { return 1; } return Integer.valueOf(this.getScore()).compareTo(other.getScore()); } }
条件分岐の問題だけど条件分岐つかわねぇw
というのをほかの言語にも起こしてみよう。
Rubyとか最近書いてないから忘れないためにもね。
def inputScores scores = [] 3.times { print "点数を入力してください: " scores << gets.to_i } return scores end scores = inputScores sum = scores.inject{ |prev, current| prev + current } avg = sum.to_f / scores.length min = scores.min max = scores.max puts "3人の点数の合計は#{sum}" puts "3人の点数の平均点は#{avg}" puts "3人の点数の最高点は#{max}" puts "3人の点数の最低点は#{min}"
次はC#3.0
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Kadai { class Program { static void Main(string[] args) { var scores = InputScores(); Console.WriteLine("3人の点数の合計は{0}", scores.Average()); Console.WriteLine("3人の点数の平均点は{0}", scores.Sum()); Console.WriteLine("3人の点数の最高点は{0}", scores.Min()); Console.WriteLine("3人の点数の最低点は{0}", scores.Max()); } static int[] InputScores() { IList<int> scores = new List<int>(); for (int i = 0; i < 3; i++) { Console.Write("点数を入力してください: "); scores.Add(int.Parse(Console.ReadLine())); } return scores.ToArray(); } } }
全部用意されてた…ってかJava大げさに書きすぎたなぁ