1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| package basic_0; import java.util.Scanner; public class BasicZc5 { public static class Student{ String name; private int age; private char sex; private double chinese; private double math; public Student(){ System.out.println("=====无参数构造器执行了====="); } public Student(String n){ System.out.println("=====有参数构造器执行了====="); } public Student(String name, int age, char sex, double chinese, double math){ this.name = name; this.age = age; this.sex = sex; this.math = math; this.chinese = chinese; } public String Name() { return name; } public void setAge(int age) { if (age > 0 && age < 200) { this.age = age; } else { System.out.println("您赋值的年龄数据非法!");}} public int getAge() { return age; } public void setChinese(double chinese) { this.chinese = chinese; } public double getChinese() { return chinese; } public double getMath() { return math; } public void setMath(double math) { this.math = math; } private Student s; public Student(Student s) { this.s = s; } public void printAllScore() { System.out.println(s.Name() + "总成绩是:" + (s.getMath() + s.getChinese())); } public void printAverageScore() { System.out.println(s.Name() + "平均成绩是:" + (s.getMath() + s.getChinese()) / 2); } } public static class Movie { private int id; private String name; private double price; private String actor; public Movie() { } public Movie(int id, String name, double price, String actor) { this.id = id; this.name = name; this.price = price; this.actor = actor; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getActor() { return actor; } public void setActor(String actor) { this.actor = actor; } } public static class MovieOperator { private Movie[] movies; public MovieOperator(Movie[] movies) { this.movies = movies; } public void printAllMovies() { System.out.println("================电影信息如下==============="); for (int i = 0; i < movies.length; i++) { Movie m = movies[i]; System.out.println( m.getId() + "\t" + m.getName() + "\t" + m.getPrice() + "\t" + m.getActor() + "\t" ); } } public void searchMovieById() { System.out.println("请输入要查询的电影id:"); Scanner sc = new Scanner(System.in); int id = sc.nextInt(); for (int i = 0; i < movies.length; i++) { Movie m = movies[i]; if (m.getId() == id) { System.out.println(m.getId() + "\t" + m.getName() + "\t" + m.getPrice() + "\t" + m.getActor() + "\t" ); return; } } System.out.println("没有找到该电影"); } } public static void main(String[] args) { Student s1 =new Student(); s1.setAge(19); System.out.println(s1.getAge()); Student s2 = new Student(); Student s3 = new Student("西门吹雪"); System.out.println("--------------------------------"); Student t1 = new Student(); t1.name = "李二"; t1.age = 22; t1.sex = '男'; t1.setChinese(92); t1.setMath(92); System.out.println(t1.name); System.out.println(t1.age); System.out.println(t1.sex); Student t2 = new Student("朱八", 18, '男', 88,88); System.out.println(t2.name); System.out.println(t2.age); System.out.println(t2.sex); Student so = new Student(t1); Student so2 = new Student(t2); so.printAllScore(); so.printAverageScore(); so2.printAllScore(); so2.printAverageScore(); System.out.println("--------------------------------"); Movie[] movies = new Movie[6]; movies[0] = new Movie(1, "让子弹飞", 9.2, "姜文"); movies[1] = new Movie(2, "战狼2", 9.2, "吴京"); movies[2] = new Movie(3, "死侍与金刚狼", 9.2,"瑞安·雷若兹"); movies[3] = new Movie(4, "逆鳞", 9.2, "沈腾"); movies[4] = new Movie(5, "美国内战", 9.2, "克斯汀·邓斯特"); movies[5] = new Movie(6, "小丑2", 9.2, "华金·菲尼克斯"); MovieOperator mo = new MovieOperator(movies); mo.printAllMovies(); mo.searchMovieById(); } }
|