//创建Students类。
package Demo2;
public class Students {
private String name;
private double English_score;
private double Math_score;
private double Java_score;
public Students() {
super();
// TODO Auto-generated constructor stub
}
public Students(String name, double english_score, double math_score, double java_score) {
super();
this.name = name;
English_score = english_score;
Math_score = math_score;
Java_score = java_score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getEnglish_score() {
return English_score;
}
public void setEnglish_score(double english_score) {
English_score = english_score;
}
public double getMath_score() {
return Math_score;
}
public void setMath_score(double math_score) {
Math_score = math_score;
}
public double getJava_score() {
return Java_score;
}
public void setJava_score(double java_score) {
Java_score = java_score;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(English_score);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(Java_score);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(Math_score);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Students other = (Students) obj;
if (Double.doubleToLongBits(English_score) != Double.doubleToLongBits(other.English_score))
return false;
if (Double.doubleToLongBits(Java_score) != Double.doubleToLongBits(other.Java_score))
return false;
if (Double.doubleToLongBits(Math_score) != Double.doubleToLongBits(other.Math_score))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "我的名字是:" + name + ", 英语成绩:" + English_score + ", 数学成绩:" + Math_score
+ ", Java成绩:" + Java_score;
}
}
//执行代码。
package Demo2;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
import org.omg.Messaging.SyncScopeHelper;
public class Test {
public static void main(String[] args) {
Collection<Students> list = new ArrayList<>();
list.add(new Students("老王", 59.05, 70, 150));
list.add(new Students("老宋", 100, 59.07, 90));
list.add(new Students("小马", 60, 100, 59.06));
list.add(new Students("小慧", 80, 80, 8));
for (Students students : list) {
System.out.println(students);
}
//
System.out.println("请选择你要改变成绩的学生姓名");
String reset = new Scanner(System.in).next();
Students look = new Students();
//
Collection<String> name_tatle = new ArrayList<>();
for (Students students : list) {
name_tatle.add(students.getName());
}
//
String sure = (name_tatle.contains(reset)) ? "已找到需要查找的学生" : "没有找到要查找的学生";
System.out.println(sure);
//
if (name_tatle.contains(reset)) {
for (Students students : list) {
if (reset.equals(students.getName())) {
look = students;
System.out.println(look);
}
}
System.out.println("请选择你要改变什么科目的成绩");
String key = new Scanner(System.in).next();
double last;
switch (key) {
case "英语":
System.out.println("请输入你要改变的成绩");
last = new Scanner(System.in).nextDouble();
look.setEnglish_score(last);
break;
case "数学":
System.out.println("请输入你要改变的成绩");
last = new Scanner(System.in).nextDouble();
look.setMath_score(last);
break;
case "java":
System.out.println("请输入你要改变的成绩");
last = new Scanner(System.in).nextDouble();
look.setJava_score(last);
break;
default:
System.out.println("没有你要查找的科目");
break;
}
for (Students students : list) {
System.out.println(students);
}
}
}
} |
|