前言:
第一次题目集比较简单,只需要掌握基础的输入输出和定义量,写的比较轻松,可能还需要一些基础语句来完成功能的实现.
第二次开始就比较难了,其中涉及到了数据的封装和多个类之间的关系,达到输入输出的要求也更加困难,可能输入能成功但输出却达不到要求.
第三次题目集虽然只有四个题目,但难度比第二次还大,不止需要加入一些新的库函数,之间的联系和运用更加复杂,完全不知道从哪里下手,导致写不出符合要求的代码.
设计与分析:
第二次题目集7-1
import java.util.Scanner;
public class Main {
static public class student{
String studentId;
String name;
int chineseScore;
int mathScore;
int physicsScore;
public student(String studentId, String name, int chineseScore, int mathScore, int physicsScore) {
this.studentId = studentId;
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.physicsScore = physicsScore;
}
public int totalScore(){
return chineseScore + mathScore + physicsScore;
}
public double averageScore(){
double average = (chineseScore + mathScore + physicsScore)/3.0;
return average;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
student[] students = new student[5];
for (int i = 0; i < 5; i++) {
String studentId = scanner.next();
String name = scanner.next();
int chineseScore = scanner.nextInt();
int mathScore = scanner.nextInt();
int physicsScore = scanner.nextInt();
students[i] = new student(studentId, name, chineseScore, mathScore, physicsScore);
}
for (student student : students) {
int totalScore = student.totalScore();
double averageScore = student.averageScore();
System.out.println(student.studentId + " " + student.name + " " + totalScore + " " + String.format("%.2f",averageScore));
}
scanner.close();
}
}
-
设计思路: 首先,定义一个学生类,包含学生的学号、姓名、语文、数学、物理成绩等属性,并且在类中定义计算总成绩和平均成绩的方法。然后,在主函数中创建一个学生数组,通过循环读取输入的学生信息,并将每个学生的信息存储在数组中。接着,遍历学生数组,调用每个学生对象的方法,计算每个学生的总成绩和平均成绩,并输出结果。
-
踩坑心得: 在编写代码过程中,可能会遇到一些细节问题,如输入信息的格式、循环的边界条件等。在这段代码中,需要注意使用Scanner类来读取输入的学生信息,确保输入的格式正确。另外,在计算平均成绩时,需要将成绩转换为double类型,以避免整数相除的截断问题。
-
主要困难: 在这段代码中,主要困难可能是对类和对象的理解以及如何正确使用Scanner类来读取输入的学生信息。此外,还需要注意在循环中正确地创建学生对象并将其存储在数组中。对于初学者来说,可能还需要注意如何正确计算和输出学生的总成绩和平均成绩。如果遇到困难,可以参考相关的教程或向他人寻求帮助。
第二次题目集7-2
import java.text.DecimalFormat;
class Score {
private int dailyScore;
private int finalScore;
public Score(int dailyScore, int finalScore) {
this.dailyScore = dailyScore;
this.finalScore = finalScore;
}
public int getTotalScore() {
return (int) (dailyScore * 0.4 + finalScore * 0.6);
}
}
class Student {
private String studentId;
private String name;
private Score chineseScore;
private Score mathScore;
private Score physicsScore;
public Student(String studentId, String name, Score chineseScore, Score mathScore, Score physicsScore) {
this.studentId = studentId;
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.physicsScore = physicsScore;
}
public int getTotalScore() {
return chineseScore.getTotalScore() + mathScore.getTotalScore() + physicsScore.getTotalScore();
}
public double getAverageScore() {
int totalScore = getTotalScore();
return (double) totalScore / 3;
}
public String getFormattedAverageScore() {
DecimalFormat df = new DecimalFormat("#.00");
return df.format(getAverageScore());
}
}
public class Main {
public static void main(String[] args) {
Score chineseScore1 = new Score(80, 90);
Score mathScore1 = new Score(85, 95);
Score physicsScore1 = new Score(75, 85);
Student student1 = new Student("001", "Alice", chineseScore1, mathScore1, physicsScore1);
Score chineseScore2 = new Score(70, 80);
Score mathScore2 = new Score(75, 85);
Score physicsScore2 = new Score(90, 95);
Student student2 = new Student("002", "Bob", chineseScore2, mathScore2, physicsScore2);
Score chineseScore3 = new Score(90, 95);
Score mathScore3 = new Score(80, 90);
Score physicsScore3 = new Score(85, 95);
Student student3 = new Student("003", "Charlie", chineseScore3, mathScore3, physicsScore3);
System.out.println(student1.getTotalScore() + " " + student1.getFormattedAverageScore());
System.out.println(student2.getTotalScore() + " " + student2.getFormattedAverageScore());
System.out.println(student3.getTotalScore() + " " + student3.getFormattedAverageScore());
}
}
-
设计思路: 本题需要设计两个类,一个是成绩类(Score),另一个是学生类(Student)。 成绩类(Score)包含两个属性:平时成绩和期末成绩。它还有一个方法用于计算总成绩,根据计算规则,使用平时成绩乘以0.4加上期末成绩乘以0.6,然后将结果转换为整数。 学生类(Student)包含五个属性:学号、姓名、语文成绩、数学成绩和英语成绩。它还有两个方法:计算总分和计算平均分。计算总分方法调用成绩类的计算总成绩方法,将三科成绩相加。计算平均分方法先计算总分,然后除以3,得到平均分。最后,我们在主函数中创建三个学生对象,分别输入学生的信息,并调用计算总分和计算平均分的方法,将结果输出。
-
踩坑心得: 在实现代码时,需要注意以下几点:
成绩类中的平时成绩和期末成绩应该是double类型,否则计算总成绩时可能会出现精度错误。
计算总成绩时,应该先将平时成绩和期末成绩乘以对应的权重再相加,否则计算结果不准确。
计算平均分时,应该将总分除以科目数,而不是固定的3,否则无法应对科目数变化的情况。
- 主要困难: 在实现代码时,最大的困难是理解题目中的计算规则,即平时成绩占40%,期末成绩占60%。如果没有理解清楚,就无法正确计算总成绩。此外,还需要注意数据类型和计算顺序等问题,否则可能会导致程序出错。
第二次题目集7-7
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Map<String, Integer> menu = new HashMap<>();
menu.put("西红柿炒蛋", 15);
menu.put("清炒土豆丝", 12);
menu.put("麻婆豆腐", 12);
menu.put("油淋生菜", 9);
Scanner scanner = new Scanner(System.in);
int totalPrice = 0;
while (true) {
String order = scanner.nextLine();
if (order.equals("end")) {
break;
}
String[] orderInfo = order.split(" ");
String dish = orderInfo[0];
int portion = Integer.parseInt(orderInfo[1]);
if (menu.containsKey(dish)) {
int basePrice = menu.get(dish);
int dishPrice = calculatePrice(basePrice, portion);
totalPrice += dishPrice;
} else {
System.out.println(dish + " does not exist");
}
}
System.out.println(totalPrice);
scanner.close();
}
private static int calculatePrice(int basePrice, int portion) {
double price = basePrice;
if (portion == 2) {
price *= 1.5;
} else if (portion == 3) {
price *= 2;
}
return (int) Math.round(price);
}
}
1. 设计思路:
本题需要设计一个简单的餐厅点餐系统。首先,我们使用一个HashMap来存储菜单,其中键为菜名,值为菜的价格。然后,我们使用一个循环来接收用户的点餐信息,直到用户输入"end"为止。在每次循环中,我们根据用户输入的菜名和份数计算出该道菜的价格,并累加到总价中。最后,我们输出总价。
2. 踩坑心得:
在实现代码时,需要注意以下几点:
使用Scanner类接收用户输入时,需要注意输入的格式和类型,比如菜名和份数之间使用空格分隔,并将份数转换为整数。
在计算菜的价格时,可以根据份数的不同进行不同的计算。这里使用了一个简单的规则:份数为1时,价格不变;份数为2时,价格乘以1.5;份数为3时,价格乘以2。可以根据实际需求进行调整。
在计算价格时,使用了Math.round()方法对结果进行四舍五入处理,然后将结果转换为整数。
3. 主要困难:
在实现代码时,最大的困难是理解题目中的要求和设计一个合理的数据结构来存储菜单信息。此外,还需要注意输入格式和类型的处理,以及价格计算的规则和四舍五入的处理。对于初学者来说,可能还需要学习和熟悉HashMap的使用方法。
第三次题目集7-2
import java.util.*;
public class Main {
public static void main(String[] args) {
// Course information map
Map<String, String[]> courseInfoMap = new HashMap<>();
// Student grades map
Map<String, List<Integer>> studentGradesMap = new HashMap<>();
Scanner scanner = new Scanner(System.in);
// Read course information
int numCourses = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < numCourses; i++) {
String[] courseInfo = scanner.nextLine().split(" ");
String courseName = courseInfo[0];
String[] courseData = Arrays.copyOfRange(courseInfo, 1, courseInfo.length);
courseInfoMap.put(courseName, courseData);
}
// Read student grades
int numStudents = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < numStudents; i++) {
String[] gradeData = scanner.nextLine().split(" ");
String studentId = gradeData[0];
String studentName = gradeData[1];
String courseName = gradeData[2];
// Check if course exists in the course information map
if (!courseInfoMap.containsKey(courseName)) {
System.out.println(studentId + " " + studentName + " : " + courseName + " does not exist");
continue;
}
String[] courseInfoArr = courseInfoMap.get(courseName);
String courseType = courseInfoArr[0];
String accessMode = courseInfoArr.length > 1 ? courseInfoArr[1] : null;
// Check if the access mode matches the course type
if (!accessModeMatches(courseType, accessMode)) {
System.out.println(studentId + " " + studentName + " : access mode mismatch");
continue;
}
int numOfGradesExpected = accessMode == null ? 1 : 2;
// Check if the number of grades matches the access mode
if (gradeData.length != 3 + numOfGradesExpected) {
System.out.println(studentId + " " + studentName + " : access mode mismatch");
continue;
}
// Parse regular score and final score
int regularScore = accessMode == null ? 0 : Integer.parseInt(gradeData[3]);
int finalScore = Integer.parseInt(gradeData[3 + numOfGradesExpected - 1]);
// Add grades to the student grades map
String gradeKey = studentId + "_" + studentName + "_" + courseName;
List<Integer> grades = studentGradesMap.getOrDefault(gradeKey, new ArrayList<>());
grades.add(regularScore);
grades.add(finalScore);
studentGradesMap.put(gradeKey, grades);
}
// Calculate and print course averages
for (String courseName : courseInfoMap.keySet()) {
String[] courseInfoArr = courseInfoMap.get(courseName);
String courseType = courseInfoArr[0];
String accessMode = courseInfoArr.length > 1 ? courseInfoArr[1] : null;
// Check if the course type matches the access mode
if (!courseTypeMatchesAccessMode(courseName, courseType, accessMode)) {
System.out.println(courseName + " : course type & access mode mismatch");
continue;
}
List<Integer> courseGrades = new ArrayList<>();
for (String studentId : studentGradesMap.keySet()) {
String[] studentInfo = studentId.split("_");
String studentName = studentInfo[1];
String studentCourse = studentInfo[2];
List<Integer> grades = studentGradesMap.get(studentId);
// Check if the student has grades for the current course
if (grades.size() < 2 || !studentCourse.equals(courseName)) {
continue;
}
// Calculate the average grade for the course
int averageGrade = calculateAverageGrade(grades);
// Print the student's average grade for the course
System.out.println(studentId + " " + studentName + " : " + courseName + " " + averageGrade);
}
}
}
// Check if the access mode matches the course type
private static boolean accessModeMatches(String courseType, String accessMode) {
if (accessMode == null) {
return true;
}
if (accessMode.equals("exam") && (courseType.equals("exam") || courseType.equals("exam_assignment"))) {
return true;
}
if (accessMode.equals("assignment") && (courseType.equals("assignment") || courseType.equals("exam_assignment"))) {
return true;
}
return false;
}
// Check if the course type matches the access mode
private static boolean courseTypeMatchesAccessMode(String courseName, String courseType, String accessMode) {
if (accessMode == null) {
return true;
}
if (accessMode.equals("exam") && (courseType.equals("exam") || courseType.equals("exam_assignment"))) {
return true;
}
if (accessMode.equals("assignment") && (courseType.equals("assignment") || courseType.equals("exam_assignment"))) {
return true;
}
return false;
}
// Calculate the average grade
private static int calculateAverageGrade(List<Integer> grades) {
int sum = 0;
for (int grade : grades) {
sum += grade;
}
return sum / grades.size();
}
}
1. 设计思路:
该程序实现了一个学生成绩管理系统。首先,通过两个HashMap来存储课程信息和学生成绩。课程信息的键为课程名,值为一个包含课程类型和访问模式的字符串数组;学生成绩的键为学生ID、学生姓名和课程名的组合,值为一个包含平时成绩和期末成绩的整数列表。然后,通过循环逐行读取输入,并根据输入的内容进行相应的处理。在处理课程信息时,将课程名作为键,课程类型和访问模式作为值存入课程信息map中。在处理学生成绩时,首先检查课程是否存在,如果不存在则输出错误信息;然后检查访问模式是否与课程类型匹配,如果不匹配则输出错误信息;接着检查成绩数量是否与访问模式匹配,如果不匹配则输出错误信息;最后,将平时成绩和期末成绩存入学生成绩map中。最后,根据课程信息map遍历计算每门课程的平均成绩,并输出学生的平均成绩。
2. 踩坑心得:
在处理学生成绩时,需要注意检查课程是否存在、访问模式是否匹配、成绩数量是否匹配的逻辑。此外,还需要注意将平时成绩和期末成绩存入学生成绩map时,使用学生ID、学生姓名和课程名的组合作为键,以保证每个学生在每门课程的成绩都能正确存储。
3. 主要困难:
主要困难在于理解并正确处理课程信息和学生成绩的数据结构,以及根据输入的要求进行相应的逻辑处理。另外,还需要注意处理输入数据时的边界情况,如输入的课程数量和学生数量。
需要研究的方向:
- 如何进行更复杂的成绩计算,如加权平均成绩或排名等。
- 如何实现更高效的数据结构和算法来存储和处理学生成绩。
- 如何进行更灵活的输入处理,以适应不同格式和规模的数据。
- 如何实现更完善的错误处理和异常处理机制,以提高程序的稳定性和可靠性。