阵列中考勤系统的打印输出

时间:2015-11-30 20:49:51

标签: java arrays joptionpane

我正在设计一个考勤系统,我的问题是我不知道如何显示输入形式的考勤。这是我的系统简介。

每个学生都有他们的矩阵编号和密码。用户将输入其矩阵编号和密码。如果矩阵编号和密码正确,系统将显示一个消息对话框。

我不知道如何保存输入然后将其显示到数组中。

源代码:

public class TESTARRAY {


    public static void main(String[] args) {

        String input1;



         // Creating array

       String[][] multi =  { {"40604", "MUHAMAD SYAMIL HAIQAL BIN KAMARUZZAMAN",""},
       {"40599","NIK MUHAMMAD 'INWAN BIN NIK ADNAN",""},
       {"37027","HARNANI BINTI ABDUL MAJID",""},
       {"37067","NURUL ASHIKIN BINTI MOHD FAZLI",""},
       {"37064","NURLIYANA BINTI MOHD KAMAL",""},
       {"37045","NADIATUL NAJIHAH BINTI MOHD NAZRI ",""},
       { "37065","NURSUHAILA BINTI MAD SALIM ",""}
       };


      printArray(multi);
   }


   public static void printArray(String[][] num1) {

       String input2;
       String input3;
       int password;
       int matricnumber;
       int a = 0;
       String  input4;



       boolean one = true;


       String[] columnNames = { "MATRIX NUMBER", "NAME","ATTENENDE"};


     String[][] input1 = new String[num1.length][num1[0].length];
      for (int i = 0; i < input1.length; i++) {
         for (int j = 0; j < input1[i].length; j++) {

             input1[i][j] = num1[i][j];
            }
           }



      DefaultTableModel model = new DefaultTableModel(input1, columnNames);
      JTable table = new JTable(model);
      JScrollPane scrollPane = new JScrollPane(table);

      JOptionPane.showMessageDialog(null, scrollPane, "CLASS ATTENDANCE",
            JOptionPane.INFORMATION_MESSAGE);






      //for (int i = 0; i < 7; i++) {

           while (one== true)
      {

         input2 = JOptionPane.showInputDialog("Please Enter Your Matric Number " );
           matricnumber = Integer.parseInt(input2);


          input3=JOptionPane.showInputDialog("Please Enter  Your Password: " );
          password = Integer.parseInt(input3);

          input4=JOptionPane.showInputDialog("Attendende : " );
          password = Integer.parseInt(input4);




          if (matricnumber==40604&&password==11){

              JOptionPane.showMessageDialog(null,"Hi "+num1[0][1]+""+num1[0][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you " );

              //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[0][1]+" ("+input2+" )");


               input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
             a = Integer.parseInt(input4);

              switch(input4){

                  case"0":one=false;
                      break;


              }


          }

          else if (matricnumber==40599&&password==22){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[1][1]+""+num1[1][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you" );

                      //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[1][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                 case"0":one=false;
                   break;
              }
          }

          else if (matricnumber==37027&&password==33){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[2][1]+""+num1[2][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you" );
                      //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[2][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

               case"0":one=false;
                break;
              }

          }

          else if (matricnumber==37067&&password==44){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[3][1]+""+num1[3][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you" );

                       //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[3][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                  case"0":one=false;
                      break;
              }

          }

          else if (matricnumber==37064&&password==55){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[4][1]+""+num1[4][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you" );
                      //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[4][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                  case"0":one=false;
                      break;
              }
          }

          else if (matricnumber==37045&&password==66){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[5][1]+""+num1[5][3]+" ("+input2+")\nYou have attend the class. Enjoy the class\nThank you" );
                      //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[5][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                  case"0":one=false;
                      break;
              }
          }

         else if (matricnumber==37065&&password==77){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[6][1]+""+num1[6][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you" );
                      //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[6][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                  case"0":one=false;
                      break;
              }

      }

      else 

                      JOptionPane.showMessageDialog(null,"You are not the student in this class. GET OUT !!!" );
           input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                  case"n":one=false;
                     break;
              }




           } 


        String input5;
         String[][] ulti =  { {"40604", "MUHAMAD SYAMIL HAIQAL BIN KAMARUZZAMAN","num1[0][2]"},
       {"40599","NIK MUHAMMAD 'INWAN BIN NIK ADNAN","num2[1][3]"},
       {"37027","HARNANI BINTI ABDUL MAJID","num2[2][3]"},
       {"37067","NURUL ASHIKIN BINTI MOHD FAZLI","num2[3][3]"},
       {"37064","NURLIYANA BINTI MOHD KAMAL","num2[4][3]"},
       {"37045","NADIATUL NAJIHAH BINTI MOHD NAZRI ","num2[5][3]"},
       { "37065","NURSUHAILA BINTI MAD SALIM ","num2[6][3]"}
       };


      printA(ulti);
           }

       public static void printA(String[][] num2){ 



       String[] columnNames = { "MATRIX NUMBER", "NAME","ATTENDENCE"};


     String[][] input5 = new String[num2.length][num2[0].length];
      for (int i = 0; i < input5.length; i++) {
         for (int j = 0; j < input5[i].length; j++) {

             input5[i][j] = num2[i][j];
            }
           }



      DefaultTableModel model = new DefaultTableModel(input5, columnNames);
      JTable table = new JTable(model);
      JScrollPane scrollPane = new JScrollPane(table);

      JOptionPane.showMessageDialog(null, scrollPane, "CLASS ATTENDANCE",
            JOptionPane.INFORMATION_MESSAGE);
   }   
   }

2 个答案:

答案 0 :(得分:1)

你应该真正将数据提取到自己的类,如:

public class User {
    String name;
    int maxtrix;
    int attend;

    public User(int matrix, String name, int attend) {
        this.name = name;
        this.maxtrix = matrix;
        this.attend = attend;
    }

    public boolean checkCredentials(int matrix, int attend) {
        if (matrix == this.maxtrix && attend == this.attend) {
            return true;
        }
        return false;
    }
}

这个数据类与列表完美配合。看看:

public static void main(String[] args) {

        List<User> users = new ArrayList<>();

        // Creating users       
        users.add(new User(40604, "MUHAMAD SYAMIL HAIQAL BIN KAMARUZZAMAN", 0));
        users.add(new User(40599, "NIK MUHAMMAD 'INWAN BIN NIK ADNAN", 0));
        users.add(new User(37027, "HARNANI BINTI ABDUL MAJID", 0));
        users.add(new User(37067, "NURUL ASHIKIN BINTI MOHD FAZLI", 0));
        users.add(new User(37064, "NURLIYANA BINTI MOHD KAMAL", 0));
        users.add(new User(37045, "NADIATUL NAJIHAH BINTI MOHD NAZRI ", 0));
        users.add(new User(37065, "NURSUHAILA BINTI MAD SALIM ", 0));

        printArray(users);
    }

现在填写表格并检查用户输入更容易(您可以遍历用户列表并使用 checkCredentials 进行检查)

答案 1 :(得分:0)

首先创建一个包含所有用户输入的数组。

public boolean searchMultiArray(user_input){

 boolean b=false;

 for(int i=0; i<multi.length; i++){

     if(java.utils.Arrays.equals(multi[i], user_input){

          b=true;
          break;
     } 
  }

 return b;
}    

然后搜索二维数组中是否存在此数组。

if (n <= 0) {return 0} else if (n <= 2) {return 1;}