java - For 循环似乎在下一次迭代中修改了对象,该对象与 if else if 结构丢失
问题描述
我正在创建一个处理学生在学校缺勤的 Java 桌面应用程序,因此我正在添加和修改由单个或一组学生造成的缺勤。
我创建了一个 Student 类,在其中我有一个 Arraylist,其中包含该学生的缺勤对象。缺勤对象代表学生缺勤的一天,这种缺勤可以是合理的也可以是不合理的(这是我前面提到的修改部分)
学生班
public class Student implements Serializable{
private String cef,nom,prenom,filliere;
private int nbHR_Justifie,nbHR_NonJustifie,nbRetards;//count of justified and unjustified days of absence and count of days that a student came late
private ArrayList<Absence> listDesAbsence;
private ArrayList<Retard> listDesRetard;
public boolean addAbsence(Absence abs){
try{
this.listDesAbsence.add(abs);
if(abs.isJustified()){
setNbHR_J(getNbHR_Justifie()+1);
}else
setNbHR_NonJustifie(getNbHR_NonJustifie()+1);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public boolean addRetard(Retard ret){//add a "student came late" object
try{
this.listDesRetard.add(ret);
this.setNbRetard(this.getNbRetard()+1);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
缺席类没什么特别的:构造函数和获取器和设置器
这是带有 JTable ( https://i.imgur.com/2OovLCM.png ) 的主 JFrame,用户应该在其中选择一个或多个学生来添加缺勤或将缺勤的性质从合理改为不合理或对面的
public class Main extends javax.swing.JFrame {
private DbConnection db;
int[] numRows=jTable1.getSelectedRows();
if(numRows.length==0){
JOptionPane.showMessageDialog(new JPanel(), "you have to select at least one student");
}else{
String [] tabCEF=new String[numRows.length];
for(int i=0;i<numRows.length;i++){
tabCEF[i]=(String) jTable1.getModel().getValueAt(numRows[i], 0);
}
RetardRegulator ar=new RetardRegulator();
ar.setTabCEF(tabCEF);
ar.setVisible(true);
现在这是一个我正在调用 AbsenceRegulator 的类,它扩展了一个 JFrame 并且应该添加或修改由在 Main JFrame 的 jTable 中选择的单个或一组学生造成的缺勤
public class AbsenceRegulator extends javax.swing.JDialog {
DbConnection db;
private String[] tabCEF;
public AbsenceRegulator() {
initComponents();
db=new DbConnection();
}
public void setTabCEF(String[] tab){
tabCEF=tab;
}
private void jLabel4MousePressed(java.awt.event.MouseEvent evt) {
boolean justified;
Absence abs;
Student s;
if(jRadioButton_NO.isSelected()){
justified=false;
}else{
justified=true;
}
if(jDateChooser1.getCalendar()!=null){
GregorianCalendar gc=(GregorianCalendar) jDateChooser1.getCalendar();
abs=new Absence(gc,jTextArea1.getText(),justified);
for(int i=0;i<tabCEF.length;i++){
s=db.getStudent(tabCEF[i]);
if(!s.dateAbsenceAlreadyTaken(gc) && !s.dateRetardAlreadyTaken(gc)){
s.addAbsence(abs);
}
else if(!s.dateAbsenceAlreadyTaken(gc) && s.dateRetardAlreadyTaken(gc)){
JOptionPane.showMessageDialog(null, "Date exite deja parmi les retards de cet etudiant");
}
else if(s.dateAbsenceAlreadyTaken(gc) && !s.dateRetardAlreadyTaken(gc)){
if(s.getAbsence(gc).isJustified() && !justified){
s.getAbsence(gc).setJustified(justified);
s.getAbsence(gc).setComment(jTextArea1.getText());
s.setNbHR_J(s.getNbHR_Justifie()-1);
s.setNbHR_NonJustifie(s.getNbHR_NonJustifie()+1);
}
else if(!s.getAbsence(gc).isJustified() && justified){
s.getAbsence(gc).setJustified(justified);
s.getAbsence(gc).setComment(jTextArea1.getText());
s.setNbHR_J(s.getNbHR_Justifie()+1);
s.setNbHR_NonJustifie(s.getNbHR_NonJustifie()-1);
}
}
}
try {
db.saveStudents();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex.toString());
}
}else{
JOptionPane.showMessageDialog(null,"vous devez entrez une date");
}
}
我向一名或多名学生插入新缺勤没有问题,(https://i.imgur.com/HNSjqxK.png)但是当我尝试修改多名学生的缺勤时,会发生一些奇怪的事情,它会在AbsenceRegulator 类中 for 循环的第一次迭代我基本上修改了所有学生中的所有特殊缺席,这使得程序跳过了最后一个 ELSE IF,这意味着相应地修改值。(https://i.imgur.com/iadJg0u.png)我知道这似乎是不可能的,但我在这里束手无策,看不出有什么问题
解决方案
推荐阅读
- php - PHP:Excel 无法打开文件,因为文件格式或文件扩展名无效
- php - 如何在登录前使用 PHP 验证密码和 pin 是否正确?
- c++ - 如何获取返回的右值引用的地址?
- sql - SQL NOT EXIST 子查询
- windows - 移动 -cmd 期间文件名和扩展名丢失
- reactjs - 从 Okta 磁贴使用单点登录时出错:无法 POST /implicit/callback
- robotframework - PywinAuto + Robot Framework 能否用于自动化 Windows GUI 应用程序和基于命令行仿真器的应用程序
- typescript - 限制两个泛型的组合
- java - 如何使用外部 java 程序在我的世界中移动我的世界角色的相机
- security - 需要查看我在 RunDeck 中的 ACL 在 RunDeck 3.1.1 中失败的位置