fork download
  1. import java.util.*;
  2.  
  3. class Gender {
  4. private int id;
  5. public Gender(int id) {
  6. this.id = id;
  7. }
  8. @Override public String toString() {
  9. switch(id) {
  10. case 0: return "女子";
  11. case 1: return "男子";
  12. default: throw new IllegalArgumentException();
  13. }
  14. }
  15. }
  16.  
  17. class Student {
  18. private int id;
  19. private String name;
  20. private Gender gender;
  21. private int nlang;
  22. private int math;
  23. private int eng;
  24. public Student(int id, String name, Gender gender, int... subs) {
  25. this.id = id;
  26. this.name = name;
  27. this.gender = gender;
  28. this.nlang = subs[0];
  29. this.math = subs[1];
  30. this.eng = subs[2];
  31. }
  32. public int getSum() { return nlang + math + eng; }
  33. public String getAve() { return String.format("%.1f", getSum() / 3.); }
  34. public String getAward() {
  35. int sum = getSum();
  36. if(sum == 300) { return "S"; }
  37. else if(270 <= sum && sum <= 299) { return "A"; }
  38. else if(240 <= sum && sum <= 269) { return "B"; }
  39. else if(180 <= sum && sum <= 239) { return "C"; }
  40. else { return "不合格"; }
  41. }
  42. @Override public String toString() {
  43. return id + "\t" + name + "\t" + gender + "\t" + nlang + "\t" + math + "\t" + eng + "\t" + getAve() + "\t" + getSum() + "\t" + getAward();
  44. }
  45. }
  46.  
  47. public class Main {
  48. private Scanner sc;
  49. private String[] subjects = { "国語", "数学", "英語" };
  50. private List<Student> students;
  51.  
  52. public Main() {
  53. this.sc = new Scanner(System.in);
  54. this.students = new ArrayList<Student>();
  55. }
  56.  
  57. private void doIt() {
  58. System.out.printf("人数を自然数で入力してください: ");
  59. int n = sc.nextInt();
  60. System.out.printf("\n");
  61. for(int i=0; i<n; i++) {
  62. Gender gender = null;
  63. String name = null;
  64. int[] subs = { 0, 0, 0 };
  65. System.out.printf("%d人目の名前を入力してください: ", (i+1));
  66. name = sc.next();
  67. System.out.printf("%d人目の性別を入力してください(0 女子, 1 男子): ", (i+1));
  68. gender = new Gender(sc.nextInt());
  69. for(int j=0; j<subjects.length; j++) {
  70. System.out.printf("%d人目の%sの成績を入力してください: ", (i+1), subjects[j]);
  71. subs[j] = sc.nextInt();
  72. }
  73. Student student = new Student(i+1, name, gender, subs);
  74. students.add(student);
  75. System.out.printf("\n");
  76. }
  77. System.out.printf("番号\t名前\t性別\t国語\t数学\t英語\t平均\t総合点\t判定\n");
  78. System.out.printf("--------------------------------------------------------------------------\n");
  79. for(Student s : students) {
  80. System.out.printf(s + "\n");
  81. }
  82. System.out.printf("--------------------------------------------------------------------------\n");
  83. }
  84.  
  85. public static void main(String[] args) { new Main().doIt(); }
  86. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty