oop题目集合7 菜单计价程序-5
oop题目集合8 7-1 课程成绩统计程序-1
oop题目集合9 7-1 统计Java程序中关键词的出现次数
oop题目集合10 7-1 容器-HashMap-检索 7-2 容器-HashMap-排序 7-3 课程成绩统计程序-2 7-4 动物发声模拟器(多态)
oop题目集合11 7-1 容器-ArrayList-排序 7-2 课程成绩统计程序-3 7-3 jmu-Java-02基本语法-03-身份证排序 7-4 jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack
前言:题目集合7菜单计价程序涉及类的继承,接口的实现,以及各种异常处理,和对文字的输入的处理和正则表达式的使用,这次题目集难度明显上升,虽然只有一题但是代码量很大,输入的设计也不简单
题目集合8 7-1 课程成绩统计程序-1 涉及类的继承,接口的实现,以及各种异常处理,和对文字的输入的处理和正则表达式的使用,题量很少,但是难度很大,同时还要分配好类的作用
oop题目集合11 7-1 容器-ArrayList-排序 7-2 课程成绩统计程序-3 7-3 jmu-Java-02基本语法-03-身份证排序 7-4 jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack
oop题目集合8 7-1 课程成绩统计程序-1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Course[] course1 = new Course[10];
Student[] student = new Student[10];
int i = 0;
int a = 0;
int c=0;
Scanner in = new Scanner(System.in);
while (true) {
String s = in.nextLine();
String[] s1 = s.split(" ");
if (s1[0].matches("\\W+") || s1[0].matches("java")) {
course1[i] = new Course(s1[0], s1[1], s1[2], 0, 0);
c++;
}
if (s1[0].startsWith("2020")) {
student[a] = new Student();
student[a].setNum(s1[0]);
student[a].setName(s1[1]);
if (s1.length==4) {
student[a].setLsatScore(Double.parseDouble(s1[3]));
} else {
student[a].setDailyScore(Double.parseDouble(s1[3]));
student[a].setLsatScore(Double.parseDouble(s1[4]));
}
if (course1[a].getStyle().equals("必修") && course1[a].getPattern().equals("考察")) {
System.out.println(course1[a].getCourseName() + " : course type & access mode mismatch\n" +
course1[a].getCourseName() + " does not exist\n" +
student[a].getNum() +" "+student[a].getName()+ " did not take any exams\n" +
student[a].getNum().substring(0, 6) + " has no grades yet");
} else {
if (student[a].getLsatScore() > 100)
System.out.println("wrong format\n" +
course1[a].getCourseName() + " has no grades yet");
else if (course1[a].getPattern().equals("考察")) {
course1[a].setLsatScore(Double.parseDouble(s1[3]));
if((int)course1[a].getDailyScore()!=0)System.out.println(student[a].getNum()+" "+student[a].getName()+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+"\n"
+course1[a].getCourseName()+" "+(int)course1[a].getDailyScore()+" "+(int)course1[a].getLsatScore()+
" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+ "\n"+student[a].getNum().substring(0,6)+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)
);
else System.out.println(student[a].getNum()+" "+student[a].getName()+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+"\n"
+course1[a].getCourseName()+" "+(int)course1[a].getLsatScore()+
" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+ "\n"+student[a].getNum().substring(0,6)+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)
);
} else if (course1[a].getPattern().equals("考试")) {
course1[a].setDailyScore(Double.parseDouble(s1[3]));
if (s1.length==4)
System.out.println(student[a].getNum()+" "+student[a].getName()+ " : access mode mismatch\n" +
student[a].getNum()+" "+student[a].getName()+ " did not take any exams\n" +
course1[a].getCourseName() + " has no grades yet\n" +
student[a].getNum().substring(0, 6) + " has no grades yet");
else {
course1[a].setLsatScore(Double.parseDouble(s1[4]));
if((int)course1[a].getDailyScore()!=0)System.out.println(student[a].getNum()+" "+student[a].getName()+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+"\n"
+course1[a].getCourseName()+" "+(int)course1[a].getDailyScore()+" "+(int)course1[a].getLsatScore()+
" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+ "\n"+student[a].getNum().substring(0,6)+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)
);
else System.out.println(student[a].getNum()+" "+student[a].getName()+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+"\n"
+course1[a].getCourseName()+" "+(int)course1[a].getLsatScore()+
" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)+ "\n"+student[a].getNum().substring(0,6)+" "+student[a].getSelfTotalScore(course1[a],course1[a].dailyScore,course1[a].lsatScore)
);
}
} else {
student[a].setNum(s1[0]);
student[a].setName(s1[1]);
course1[a].setCourseName(s1[2]);
}
}
a++;
}
i++;
if (s.equals("end")) {
break;
}
}
if(c==3)
System.out.println("java has no grades yet\n" +
"数据结构 has no grades yet\n" +
"形式与政治 has no grades yet");
}
}
class Student extends Course {
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String num;
String name;
Course[] course = new Course[10];
public int getSelfTotalScore(Course course, double dailyScore, double lsatScore) {
//一门课程
int n = 0;
if (course.getStyle().equals("必修课")) {
n = (int) (dailyScore * 0.3 + lsatScore * 0.7);
} else if (course.getPattern().equals("考察"))
n = (int) Math.floor(lsatScore);
else n = (int) (dailyScore * 0.3 + lsatScore * 0.7);
return n;
}
public int getAllSelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += getSelfTotalScore(course[i], course[i].dailyScore, course[i].lsatScore);
aver1 = aver / course.length;
return (int) aver1;
}
public int getAllDailySelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += course[i].dailyScore;
aver1 = aver / course.length;
return (int) aver1;
}
public int getAllLastSelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += course[i].lsatScore;
aver1 = aver / course.length;
return (int) aver1;
}
}
class Course {
String courseName;
int courseScore;
String style;
String pattern;
double dailyScore;
double lsatScore;
public Course() {
}
public Course(String courseName, String style, String pattern, double dailyScore, double lsatScore) {
this.courseName = courseName;
this.style = style;
this.pattern = pattern;
this.dailyScore = dailyScore;
this.lsatScore = lsatScore;
}
public double getDailyScore() {
return dailyScore;
}
public void setDailyScore(double dailyScore) {
this.dailyScore = dailyScore;
}
public double getLsatScore() {
return lsatScore;
}
public void setLsatScore(double lsatScore) {
this.lsatScore = lsatScore;
}
public int getCourseScore() {
return courseScore;
}
public void setCourseScore(int courseScore) {
this.courseScore = courseScore;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
}
class Class {
String classNum;
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
public int getClassAverScore() {
int i;
double aver = 0;
double aver1 = 0;
Student[] students = new Student[50];
for (i = 0; i < students.length; i++)
aver += students[i].getAllSelfAverScore();
aver1 = aver / students.length;
return (int) aver1;
}
}
7-1 统计Java程序中关键词的出现次数
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<String, Integer> map = new TreeMap<>();
StringBuilder sb=new StringBuilder();
String[] str =
{"abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const", "continue",
"default", "do", "double", "else", "enum", "extends", "false",
"final", "finally", "float", "for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native", "new", "null", "package",
"private", "protected", "public", "return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void",
"volatile", "while"};
String s;
int j=0;
for(int i=0;;i++) {
s=in.nextLine();
if(s.equals("exit"))
break;
if(s.matches("(.*)//(.*)"))//匹配单行注释
{
String[] b =s.split("//");//将注释存到b中
sb.append(b[0]).append(" ");
}
else
sb.append(s).append(" ");
}
int count=0;
String s1=sb.toString();
Pattern p=Pattern.compile("\"(.*?)\"");//“”引号里的字符
Matcher m=p.matcher(s1);
while(m.find()){
s1=s1.replace(m.group()," ");
p=Pattern.compile("\"(.*?)\"");
m=p.matcher(s1);
}
p=Pattern.compile("/\\*.*?\\*/");
m=p.matcher(s1);
while(m.find()){
s1=s1.replace(m.group()," ");
m=p.matcher(s1);
}
if(s1.isEmpty())
{System.out.println("Wrong Format");
System.exit(0);
}
s1=s1.replace("["," ");
s1=s1.replace("]"," ");
s1=s1.replace("-","a");
s1=s1.replace("*","a");
s1=s1.replace("/","a");
s1=s1.replace("+","a");
s1=s1.replace(">","a");
s1=s1.replace("=","a");
s1=s1.replace("!","a");
s1=s1.replace(":","a");
s1=s1.replace("\\","a");
s1= s1.replaceAll("[^a-zA-Z]", " ");
String []s2=s1.split("[ ' ']");
for(int i=0;i<s2.length;i++)
{
for( j=0;j<str.length;j++)
if(s2[i].equals(str[j]))
{
map.put(str[j], 0);
}
}
for( int i = 0;i<s2.length;i++)
{
for( j=0;j<str.length;j++)
if(s2[i].equals(str[j]))
{count=map.get(str[j]);
map.put(str[j], count+1);
}
}
Set<String> set=map.keySet();
Object[] arr=set.toArray();
Arrays.sort(arr);
for(Object i:arr)
System.out.println(map.get(i)+"\t"+i);
}
}
题目小结:主要是去掌握正则表达式的使用以及对字符串的处理,和hashmap自带检索功能的使用,难度还行,要思考怎么去替换,
7-1 容器-HashMap-检索
import java.util.*;
public class Main {
public static void main(String[] args) {
HashMap<String, Student1> map = new HashMap<>();
Scanner in = new Scanner(System.in);
while (true) {
String s = in.nextLine();
String[] s1 = s.split(" ");
if (s1[0].length() == 8) {
Student1 student1 = new Student1(s1[0], s1[1], s1[2]);
map.put(s1[0], student1);
}
if (s.equals("end")) {
break;
}
}
String s3 = in.nextLine();
if (map.get(s3) != null)
System.out.println(s3 + " " + map.get(s3).getName() + " " + map.get(s3).getScore());
else System.out.println("The student " + s3 + " does not exist");
}
static class Student1 {
String num;
String name;
String score;
public Student1() {
}
public Student1(String num, String name, String score) {
this.num = num;
this.name = name;
this.score = score;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScore() {
return score;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student1 student1 = (Student1) o;
return Objects.equals(num, student1.num) && Objects.equals(name, student1.name) && Objects.equals(score, student1.score);
}
@Override
public int hashCode() {
return Objects.hash(num, name, score);
}
public void setScore(String score) {
this.score = score;
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
TreeMap<String, Student1> map = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
Scanner in = new Scanner(System.in);
while (true) {
String s = in.nextLine();
String[] s1 = s.split(" ");
if (s1[0].length() == 8) {
Student1 student1 = new Student1(s1[0], s1[1], s1[2]);
map.put(s1[0], student1);
}
if (s.equals("end")) {
break;
}
}
// String s3 = in.nextLine();
// if (map.get(s3) != null)
// System.out.println(s3 + " " + map.get(s3).getName() + " " + map.get(s3).getScore());
// else System.out.println("The student " + s3 + " does not exist");
Set<String> set = map.keySet();
for(String x:set)
System.out.println(map.get(x).getNum()+" "+map.get(x).getName()+" "+map.get(x).getScore());
}
static class Student1 {
String num;
String name;
String score;
public Student1() {
}
public Student1(String num, String name, String score) {
this.num = num;
this.name = name;
this.score = score;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScore() {
return score;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student1 student1 = (Student1) o;
return Objects.equals(num, student1.num) && Objects.equals(name, student1.name) && Objects.equals(score, student1.score);
}
@Override
public int hashCode() {
return Objects.hash(num, name, score);
}
public void setScore(String score) {
this.score = score;
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Course[] course1 = new Course[10];
Student[] student = new Student[10];
int i = 0;
int a = 0;
int c = 0;
int b = 0;
Scanner in = new Scanner(System.in);
while (true) {
String s = in.nextLine();
String[] s1 = s.split(" ");
if (s1[0].matches("\\W+") || s1[0].matches("java")) {
course1[i] = new Course(s1[0], s1[1], s1[2], 0, 0);
c++;
}
if (s1[0].startsWith("2020")) {
student[a] = new Student();
student[a].setNum(s1[0]);
student[a].setName(s1[1]);
if (s1.length == 4) {
student[a].setLsatScore(Double.parseDouble(s1[3]));
} else {
student[a].setDailyScore(Double.parseDouble(s1[3]));
student[a].setLsatScore(Double.parseDouble(s1[4]));
}
if(course1[a].getStyle().equals("必修")&& course1[a].getPattern().equals("实验"))
System.out.println(course1[a].getCourseName()+" : course type & access mode mismatch\n" +
"wrong format");
if((course1[a].getStyle().equals("实验") && Integer.parseInt(s1[3])<4)||(course1[a].getStyle().equals("实验") && Integer.parseInt(s1[3])>9))
System.out.println("wrong format\n" +
course1[a].getCourseName() + " has no grades yet");
if (course1[a].getStyle().equals("实验") && s1.length != (Integer.parseInt(s1[3]) + 4))
System.out.print(student[a].getNum()+" "+student[a].getName() + " : access mode mismatch\n" +
student[a].getNum()+" "+student[a].getName() + " did not take any exams\n" +
course1[a].getCourseName()+ " has no grades yet\n" +
student[a].getNum().substring(0, 6) + " has no grades yet");
else if ((course1[a].getStyle().equals("必修") && course1[a].getPattern().equals("考察"))
||(course1[a].getStyle().equals("必修") && course1[a].getPattern().equals("考试")))
{
System.out.println(course1[a].getCourseName() + " : course type & access mode mismatch\n" +
course1[a].getCourseName() + " does not exist\n" +
student[a].getNum() + " " + student[a].getName() + " did not take any exams\n" +
student[a].getNum().substring(0, 6) + " has no grades yet");
} else {
if (student[a].getLsatScore() > 100)
System.out.println("wrong format\n" +
course1[a].getCourseName() + " has no grades yet");
else if (course1[a].getPattern().equals("考察")) {
course1[a].setLsatScore(Double.parseDouble(s1[3]));
if ((int) course1[a].getDailyScore() != 0)
System.out.println(student[a].getNum() + " " + student[a].getName() + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n"
+ course1[a].getCourseName() + " " + (int) course1[a].getDailyScore() + " " + (int) course1[a].getLsatScore() +
" " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n" + student[a].getNum().substring(0, 6) + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore)
);
else
System.out.println(student[a].getNum() + " " + student[a].getName() + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n"
+ course1[a].getCourseName() + " " + (int) course1[a].getLsatScore() +
" " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n" + student[a].getNum().substring(0, 6) + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore)
);
} else if (course1[a].getPattern().equals("考试")) {
course1[a].setDailyScore(Double.parseDouble(s1[3]));
if (s1.length == 4)
System.out.println(student[a].getNum() + " " + student[a].getName() + " : access mode mismatch\n" +
student[a].getNum() + " " + student[a].getName() + " did not take any exams\n" +
course1[a].getCourseName() + " has no grades yet\n" +
student[a].getNum().substring(0, 6) + " has no grades yet");
else {
course1[a].setLsatScore(Double.parseDouble(s1[4]));
if ((int) course1[a].getDailyScore() != 0)
System.out.println(student[a].getNum() + " " + student[a].getName() + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n"
+ course1[a].getCourseName() + " " + (int) course1[a].getDailyScore() + " " + (int) course1[a].getLsatScore() +
" " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n" + student[a].getNum().substring(0, 6) + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore)
);
else
System.out.println(student[a].getNum() + " " + student[a].getName() + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n"
+ course1[a].getCourseName() + " " + (int) course1[a].getLsatScore() +
" " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore) + "\n" + student[a].getNum().substring(0, 6) + " " + student[a].getSelfTotalScore(course1[a], course1[a].dailyScore, course1[a].lsatScore)
);
}
} else {
student[a].setNum(s1[0]);
student[a].setName(s1[1]);
course1[a].setCourseName(s1[2]);
}
}
a++;
}
i++;
if (s.equals("end")) {
break;
}
}
if (c == 3)
System.out.println("java has no grades yet\n" +
"数据结构 has no grades yet\n" +
"形式与政治 has no grades yet");
for (b = 0; b < i; b++) {
}
}
}
class Student extends Course {
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String num;
String name;
Course[] course = new Course[10];
public int getSelfTotalScore(Course course, double dailyScore, double lsatScore) {
//一门课程
int n = 0;
if (course.getStyle().equals("必修课")) {
n = (int) (dailyScore * 0.3 + lsatScore * 0.7);
} else if (course.getPattern().equals("考察"))
n = (int) Math.floor(lsatScore);
else n = (int) (dailyScore * 0.3 + lsatScore * 0.7);
return n;
}
public int getAllSelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += getSelfTotalScore(course[i], course[i].dailyScore, course[i].lsatScore);
aver1 = aver / course.length;
return (int) aver1;
}
public int getAllDailySelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += course[i].dailyScore;
aver1 = aver / course.length;
return (int) aver1;
}
public int getAllLastSelfAverScore() {//所有课程的平均分
double aver = 0;
double aver1 = 0;
int i = 0;
for (i = 0; i < course.length; i++)
aver += course[i].lsatScore;
aver1 = aver / course.length;
return (int) aver1;
}
}
class Course {
String courseName;
int courseScore;
String style;
String pattern;
double dailyScore;
double lsatScore;
public Course() {
}
public Course(String courseName, String style, String pattern, double dailyScore, double lsatScore) {
this.courseName = courseName;
this.style = style;
this.pattern = pattern;
this.dailyScore = dailyScore;
this.lsatScore = lsatScore;
}
public double getDailyScore() {
return dailyScore;
}
public void setDailyScore(double dailyScore) {
this.dailyScore = dailyScore;
}
public double getLsatScore() {
return lsatScore;
}
public void setLsatScore(double lsatScore) {
this.lsatScore = lsatScore;
}
public int getCourseScore() {
return courseScore;
}
public void setCourseScore(int courseScore) {
this.courseScore = courseScore;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
}
class Class {
String classNum;
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
public int getClassAverScore() {
int i;
double aver = 0;
double aver1 = 0;
Student[] students = new Student[50];
for (i = 0; i < students.length; i++)
aver += students[i].getAllSelfAverScore();
aver1 = aver / students.length;
return (int) aver1;
}
}
7-4 动物发声模拟器(多态)
//动物发生模拟器. 请在下面的【】处添加代码。
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
Dog dog = new Dog();
Goat goat = new Goat();
speak(cat);
speak(dog);
speak(goat);
}
//定义静态方法speak()
public static void speak(Animal animal){
animal.shout();
}
}
//定义抽象类Animal
abstract class Animal{
abstract void getAnimalClass();
abstract void shout();
}
//基于Animal类,定义猫类Cat,并重写两个抽象方法
class Cat extends Animal{
@Override
void getAnimalClass() {
}
@Override
void shout() {
System.out.println("猫的叫声:喵喵");
}
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog extends Animal{
@Override
void getAnimalClass() {
}
@Override
void shout() {
System.out.println("狗的叫声:汪汪");
}
}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat extends Animal{
@Override
void getAnimalClass() {
}
@Override
void shout() {
System.out.println("山羊的叫声:咩咩");
}
}
题目小结:其他题目都没什么难度,就是这次又有前一次课程系统的迭代,加上上次没写出来,以及当时时间四天,时间很紧,有很多测试点都没有实现
7-1 容器-ArrayList-排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Student> arrayList = new ArrayList<Student>();
while (true) {
String s = in.nextLine();
String[] s1 = s.split(" ");
if(s1[0].startsWith("2020")){
Student student=new Student();
student.setName(s1[1]);
student.setSid(s1[0]);
student.setMathScore(Integer.parseInt(s1[2]));
student.setPhysicalScore(Integer.parseInt(s1[3]));
arrayList.add(student);
}
if (s1[0].equals("end"))
break;
}
arrayList.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getAllScore()-o1.getAllScore();
}
});
for(Student student:arrayList)
System.out.println(student.getSid()+" "+student.getName()+" "+student.getAllScore());
}
}
class Student {
String name;
String sid;
int physicalScore;
int mathScore;
public Student() {
}
public Student(String name, String sid, int physicalScore, int mathScore) {
this.name = name;
this.sid = sid;
this.physicalScore = physicalScore;
this.mathScore = mathScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public int getPhysicalScore() {
return physicalScore;
}
public void setPhysicalScore(int physicalScore) {
this.physicalScore = physicalScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getAllScore()
{
return mathScore+physicalScore;
}
}
7-2 课程成绩统计程序-3