for (String id : ids) { //左边 String left = "1,4,7,10,13,16"; String[] leftArr = left.split(","); for (String s : leftArr) { if (s.equals(id)) { localName = "左侧"; break; } } //中间 String middle = "2,5,8,11,14,17"; String[] middleArr = middle.split(","); for (String s : middleArr) { if (s.equals(id)) { localName = "中间"; break; } } //右边 String right = "3,6,9,12,15,18"; String[] rightArr = right.split(","); for (String s : rightArr) { if (s.equals(id)) { localName = "右侧"; break; } } titleCell.setCellValue(damName + localName+"浸润线监测点"+id); } 我想通过id和s比较是否相等然后赋值对应的位置
时间: 2024-02-10 12:18:08 浏览: 130
你可以使用 if 语句对 id 和 s 进行比较,如果相等,就赋值对应的位置。例如:
```
if (id.equals(s)) {
if (Arrays.asList(leftArr).contains(s)) {
localName = "左侧";
} else if (Arrays.asList(middleArr).contains(s)) {
localName = "中间";
} else if (Arrays.asList(rightArr).contains(s)) {
localName = "右侧";
}
titleCell.setCellValue(damName + localName + "浸润线监测点" + id);
}
```
这里我们使用了 Arrays.asList() 方法将数组转换为 List,然后使用 contains() 方法来判断该 List 是否包含指定的元素 s。如果包含,就赋值对应的位置。
相关问题
for (String id : ids) { //左边 String left = "1,4,7,10,13,16"; String[] leftArr = left.split(","); for (String s : leftArr) { if (s.equals(id)) { localName = "左侧"; break; } } //中间 String middle = "2,5,8,11,14,17"; String[] middleArr = middle.split(","); for (String s : middleArr) { if (s.equals(id)) { localName = "中间"; break; } } //右边 String right = "3,6,9,12,15,18"; String[] rightArr = right.split(","); for (String s : rightArr) { if (s.equals(id)) { localName = "右侧"; break; } } titleCell.setCellValue(damName + localName+"浸润线监测点"+id); } 给我优化一下代码
可以使用一个数组来存储每个位置的名称,然后遍历该数组,判断当前id属于哪个位置,从而得到对应的位置名称。这样可以避免多次重复的循环和字符串拆分,提高代码效率。代码示例如下:
```
String[] locations = {"左侧", "中间", "右侧"};
String[] ids = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"};
for (String id : ids) {
int index = (Integer.parseInt(id) - 1) % 3; //计算id所属位置的数组下标
String localName = locations[index];
titleCell.setCellValue(damName + localName + "浸润线监测点" + id);
}
```
这样就可以实现同样的功能,但是代码更加简洁和高效。
补全以下代码private String cid;// Course id, e.g., CS110. private String name;// Course name, e.g., Introduce to Java Programming. private Integer credit;// Credit of this course private GradingSchema gradingSchema; //Grading schema of this course // enum GradingSchema{FIVE_LEVEL, PASS_FAIL} private Integer capacity;// Course capacity. private Integer leftCapacity;// Course capacity left. You should update the left capacity when enrolling students. private Set<Timeslot> timeslots;// One course may have one or more timeslots. e.g., a lecture in Monday's 10:20-12:10, and a lab in Tuesday's 14:00-15:50. public Course(String cid, String name, Integer credit, GradingSchema gradingSchema, Integer capacity) // constructor public void addTimeslot(Timeslot timeslot) //Record a timeslot for this course private Integer id;// A unique student id, should be an 8-digit integer: Undergraduates' ids should start with 1; Postgraduates' ids should start with 3. e.g., 12213199. private String name;// Student’s name private Map<Course, Grade> courses;// Enrolled courses, using Map structure to store course and its grade as a pair. Grade is an enum type enum Grade{PASS,FAIL,A,B,C,D,F}with an attribute: Double gradePoint protected Student(Integer id, String name) // constructor public abstract boolean canGraduate() // Checks if this student satisfies all the graduating conditions. Hint: you are allowed to change this abstract method into non-abstract to check if the student satisfies the common graduation conditions. public void enroll(Course course) // Tries to enroll the course, do some checks before enrolling. public void recordGrade(Course course, Grade grade)// Records the grade of a course that is current learning. public double getGpa() // Calculates the GPA for this student. public UndergraduateStudent(Integer id, String name)// constructor public boolean canGraduate() //Additional graduating conditions for undergraduate students public PostgraduateStudent(Integer id, String name)// constructor public boolean canGraduate() //Additional graduating conditions for postgraduate students
以下是补全后的代码:
public class Course {
private String cid;
private String name;
private Integer credit;
private GradingSchema gradingSchema;
private Integer capacity;
private Integer leftCapacity;
private Set<Timeslot> timeslots;
public Course(String cid, String name, Integer credit, GradingSchema gradingSchema, Integer capacity) {
this.cid = cid;
this.name = name;
this.credit = credit;
this.gradingSchema = gradingSchema;
this.capacity = capacity;
this.leftCapacity = capacity;
this.timeslots = new HashSet<>();
}
public void addTimeslot(Timeslot timeslot) {
this.timeslots.add(timeslot);
}
}
public enum GradingSchema {
FIVE_LEVEL,
PASS_FAIL
}
public class Student {
private Integer id;
private String name;
private Map<Course, Grade> courses;
public Student(Integer id, String name) {
this.id = id;
this.name = name;
this.courses = new HashMap<>();
}
public abstract boolean canGraduate();
public void enroll(Course course) {
if (course.getLeftCapacity() > 0) {
this.courses.put(course, null);
course.decreaseLeftCapacity();
}
}
public void recordGrade(Course course, Grade grade) {
if (this.courses.containsKey(course)) {
this.courses.put(course, grade);
}
}
public double getGpa() {
double totalGradePoints = 0;
double totalCredit = 0;
for (Map.Entry<Course, Grade> entry : this.courses.entrySet()) {
Course course = entry.getKey();
Grade grade = entry.getValue();
if (grade != null) {
double gradePoint = grade.getGradePoint(course.getGradingSchema());
totalGradePoints += gradePoint * course.getCredit();
totalCredit += course.getCredit();
}
}
return totalCredit > 0 ? totalGradePoints / totalCredit : 0;
}
}
public enum Grade {
PASS(4.0),
FAIL(0),
A(4.0),
B(3.0),
C(2.0),
D(1.0),
F(0);
private double gradePoint;
Grade(double gradePoint) {
this.gradePoint = gradePoint;
}
public double getGradePoint(GradingSchema gradingSchema) {
if (gradingSchema == GradingSchema.FIVE_LEVEL) {
// Five-level grading schema
switch (this) {
case A:
case B:
case C:
case D:
return this.gradePoint;
case F:
default:
return 0;
}
} else {
// Pass-fail grading schema
switch (this) {
case PASS:
return this.gradePoint;
case FAIL:
default:
return 0;
}
}
}
}
public class UndergraduateStudent extends Student {
public UndergraduateStudent(Integer id, String name) {
super(id, name);
}
@Override
public boolean canGraduate() {
// Additional graduating conditions for undergraduate students
// TODO: implement this method
return false;
}
}
public class PostgraduateStudent extends Student {
public PostgraduateStudent(Integer id, String name) {
super(id, name);
}
@Override
public boolean canGraduate() {
// Additional graduating conditions for postgraduate students
// TODO: implement this method
return false;
}
}
public class Timeslot {
private DayOfWeek dayOfWeek;
private LocalTime startTime;
private LocalTime endTime;
public Timeslot(DayOfWeek dayOfWeek, LocalTime startTime, LocalTime endTime) {
this.dayOfWeek = dayOfWeek;
this.startTime = startTime;
this.endTime = endTime;
}
}
阅读全文