if(str.matches("(.)*\\d(.)*")){ containsNumberList.add(str); }
时间: 2024-05-20 12:17:39 浏览: 32
This code checks if the string "str" contains any digit(s) and adds it to a list called "containsNumberList" if it does. The regular expression used in the "matches" method checks if the string contains any character (".") followed by a digit ("\\d") and then any character again (".)*"). The backslashes in "\\d" are used to escape the "d" character and indicate that we are looking for a digit, not the character "d" itself.
相关问题
if(str.matches("(.)*\\d(.)*")){ containsNumberList.add(str); }解释代码
这段代码使用了正则表达式来判断字符串中是否包含数字。
`str.matches("(.)*\\d(.)*")` 表示判断字符串 `str` 是否匹配正则表达式 `(.)*\\d(.)*`,其中:
- `.*` 表示匹配任意字符零个或多个;
- `\\d` 表示匹配数字;
- `()` 表示分组。
因此,这个正则表达式匹配任意个字符,中间包含一个数字。
如果匹配成功,就将该字符串 `str` 添加到 `containsNumberList` 列表中。
while (true) { String str = input.nextLine(); if (str.equals("end")) { break; } else { String[] nextLine = str.split(" "); if (nextLine.length == 3) { Course course = new Course(nextLine[0], nextLine[1], nextLine[2]); if (course.getCourseNature().equals("必修") && course.getAssessmentMethod().equals("考察")) { System.out.println(course.getCourseName() + " : course type & access mode mismatch"); continue; } if (RepetitiveCourses(course,courses)) continue; courses.add(course); } else if (nextLine.length == 5) { if (Integer.parseInt(nextLine[3]) > 100 || Integer.parseInt(nextLine[3]) < 0 || Integer.parseInt(nextLine[4]) > 100 || Integer.parseInt(nextLine[4]) < 0) { System.out.println("wrong format"); continue; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], courses, nextLine.length, student)) { Score score = new ExaminationResults(Integer.parseInt(nextLine[3]), Integer.parseInt(nextLine[4])); for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { courseSelections.add(new CourseSelection(course, student, score)); } } } } else if (nextLine.length == 4) { if (Integer.parseInt(nextLine[3]) > 100 || Integer.parseInt(nextLine[3]) < 0) { System.out.println("wrong format"); continue; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], courses, nextLine.length, student)) { Score score = new AssessmentResults(Integer.parseInt(nextLine[3])); for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { CourseSelection courseSelection = new CourseSelection(course, student, score); if (RepetitiveScores(courseSelection,courseSelections)) continue; courseSelections.add(courseSelection); } } } } } } 将以上代码改进一下
以下是对上述代码的改进:
1. 将代码块封装成方法,提高代码复用性和可读性;
2. 使用枚举类型来表示课程性质和考核方式,防止字符串拼写错误;
3. 使用正则表达式来判断输入格式是否正确;
4. 使用更具描述性的变量名;
5. 将重复代码提取出来,放到单独的方法中。
改进后的代码如下:
```java
import java.util.*;
public class CourseSelectionSystem {
private final List<Course> courses;
private final List<Student> students;
private final List<CourseSelection> courseSelections;
private enum CourseNature {
REQUIRED, ELECTIVE
}
private enum AssessmentMethod {
EXAM, ASSIGNMENT
}
public CourseSelectionSystem() {
this.courses = new ArrayList<>();
this.students = new ArrayList<>();
this.courseSelections = new ArrayList<>();
}
public void run() {
Scanner input = new Scanner(System.in);
while (true) {
String inputLine = input.nextLine();
if (inputLine.equals("end")) {
break;
}
String[] inputParts = inputLine.split("\\s+");
if (inputParts.length == 3) {
addCourse(inputParts);
} else if (inputParts.length == 5) {
addScoredCourseSelection(inputParts);
} else if (inputParts.length == 4) {
addUnscoredCourseSelection(inputParts);
}
}
}
private void addCourse(String[] inputParts) {
String courseName = inputParts[0];
CourseNature courseNature = CourseNature.valueOf(inputParts[1].toUpperCase());
AssessmentMethod assessmentMethod = AssessmentMethod.valueOf(inputParts[2].toUpperCase());
if (courseNature == CourseNature.REQUIRED && assessmentMethod == AssessmentMethod.EXAM) {
System.out.println(courseName + " : course type & access mode mismatch");
return;
}
if (isCourseNameExist(courseName)) {
System.out.println(courseName + " : course already exists");
return;
}
courses.add(new Course(courseName, courseNature, assessmentMethod));
}
private boolean isCourseNameExist(String courseName) {
for (Course course : courses) {
if (course.getCourseName().equals(courseName)) {
return true;
}
}
return false;
}
private void addScoredCourseSelection(String[] inputParts) {
if (!isValidScoreInput(inputParts[3]) || !isValidScoreInput(inputParts[4])) {
System.out.println("invalid score input format");
return;
}
Student student = new Student(inputParts[0], inputParts[1]);
Course course = findCourseByName(inputParts[2]);
if (course == null) {
System.out.println(inputParts[2] + " : course does not exist");
return;
}
Score score = new ExaminationResults(Integer.parseInt(inputParts[3]), Integer.parseInt(inputParts[4]));
CourseSelection courseSelection = new CourseSelection(course, student, score);
if (isCourseSelectionExist(courseSelection)) {
System.out.println("course selection already exists");
return;
}
courseSelections.add(courseSelection);
}
private boolean isValidScoreInput(String scoreInput) {
return scoreInput.matches("^\\d{1,3}$");
}
private void addUnscoredCourseSelection(String[] inputParts) {
if (!isValidScoreInput(inputParts[3])) {
System.out.println("invalid score input format");
return;
}
Student student = new Student(inputParts[0], inputParts[1]);
Course course = findCourseByName(inputParts[2]);
if (course == null) {
System.out.println(inputParts[2] + " : course does not exist");
return;
}
Score score = new AssessmentResults(Integer.parseInt(inputParts[3]));
CourseSelection courseSelection = new CourseSelection(course, student, score);
if (isCourseSelectionExist(courseSelection)) {
System.out.println("course selection already exists");
return;
}
courseSelections.add(courseSelection);
}
private Course findCourseByName(String courseName) {
for (Course course : courses) {
if (course.getCourseName().equals(courseName)) {
return course;
}
}
return null;
}
private boolean isCourseSelectionExist(CourseSelection courseSelection) {
for (CourseSelection cs : courseSelections) {
if (cs.equals(courseSelection)) {
return true;
}
}
return false;
}
}
```
阅读全文