第三周作业

来源:互联网 发布:电视盒子直播软件 编辑:程序博客网 时间:2024/06/07 00:48
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //判断是否为闰年。  
  2.   
  3. import javax.swing.JOptionPane;    
  4.   
  5. public class LeapYear    
  6. {    
  7.     public static void main(String args[])    
  8.     {    
  9.         int year = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入一个年份"));    
  10.         if((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 400 == 0))    
  11.         {    
  12.             JOptionPane.showMessageDialog(null,year + " 年是闰年!");    
  13.         }         else    
  14.         {            JOptionPane.showMessageDialog(null,year + " 年不是闰年!");    
  15.         }    
  16.     }    
  17. }    


   

  

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //图形界面学生签到程序。  
  2.   
  3. import java.awt.Button;    
  4. import java.awt.FlowLayout;    
  5. import java.awt.Frame;    
  6. import java.awt.Label;    
  7. import java.awt.TextArea;    
  8. import java.awt.TextField;    
  9. import java.awt.event.*;    
  10. import java.io.File;    
  11. import java.io.FileNotFoundException;    
  12. import java.io.PrintWriter;    
  13. import java.text.SimpleDateFormat;    
  14. import java.util.Date;    
  15. import java.util.Scanner;      
  16. import javax.swing.JFrame;     
  17. public  class RegisterDemo extends Frame implements ActionListener   
  18. {    
  19.     static String[][] stringArray ;   
  20.     static JFrame jf = new JFrame("GUI example");//JFrame容器    
  21.     static Label lb = new Label("学生姓名");    
  22.     static TextField tf = new TextField(10);    
  23.     static Button b1 = new Button("到");    
  24.     static Button b2 = new Button("缺席");    
  25.     static Label b = new Label("缺到名单");    
  26.     static TextArea ta = new TextArea(15,30);    
  27.     static int count2 = 0;//记录学生人数    
  28.     static int count = 0;//记录学生人数    
  29.     static String strAbsent = " ";//记录缺席的学生姓名    
  30.   
  31.     public static void main(String[] args) throws Exception {    
  32.         File fileinput=new File("E:\\wl121.txt");  //输入文件    
  33.         Scanner sc = new Scanner(fileinput);  //构造新的Scanner,指定fileinput为其生成值    
  34.         Scanner st = new Scanner(fileinput);  //同上    
  35.   
  36.         while (sc.hasNext()) //确定人数      
  37.         {    
  38.             count++;      
  39.             sc.nextLine();      
  40.         }    
  41.         stringArray = new String[count + 1][2];//给数组stringArray加多一位,后面用得上    
  42.         for (int a = 0; a < count ; a++)//读入数组      
  43.         {           
  44.             stringArray[a][0] = st .nextLine();    
  45.         }      
  46.         sc.close();     
  47.         st.close();    
  48.   
  49.         stringArray[count][0] = "点名结束";    
  50.         tf.setText(stringArray[count2][0]);//在文本框中加入数组的第一行内容    
  51.   
  52.         RegisterDemo tu = new RegisterDemo();//构造方法    
  53.   
  54.         jf.setLayout(new FlowLayout(FlowLayout.LEFT,20,40));//布局管理器    
  55.         jf.add(lb);    
  56.         jf.add(tf);    
  57.         jf.add(b1);    
  58.         jf.add(b2);    
  59.         jf.add(b);    
  60.         jf.add(ta);    
  61.         jf.setSize(350,450);//设定大小    
  62.         jf.setVisible(true);//容器可见    
  63.         b1.addActionListener(tu);//监听按钮    
  64.         b2.addActionListener(tu);//监听按钮    
  65.     }    
  66.     public  void actionPerformed(ActionEvent e)//监听事件    
  67.     {    
  68.   
  69.         if(e.getSource()==b1 && count2 < count)//点击“到”按钮并且总点击数少于人数时,往下执行  
  70.         {                 
  71.             count2++;                                             
  72.             tf.setText(stringArray[count2][0]);//在文本框中加入数组的内容    
  73.   
  74.         }    
  75.         else if(e.getSource()==b2 && count2 < count)//点击“缺席”按钮并且总点击数少于人数时,往下执行    
  76.         {    
  77.             strAbsent = strAbsent + stringArray[count2][0] + "\n";//把缺席的名字记录到strAbsent中    
  78.             count2++;//    
  79.             tf.setText(stringArray[count2][0]);//在文本框中加入数组的内容    
  80.         }    
  81.         if(count2 == count)//当读完了所有名字后,把缺席名单显示在文本域中    
  82.         {    
  83.             ta.setText(strAbsent);    
  84.             ta.getSelectedText();                  
  85.         }    
  86.         Date now = new Date();//获得当前日期,以便在每天考勤后自动生成不同名字的考勤结果文件    
  87.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHMM");//方便修改日期格式      
  88.         String strDate = dateFormat.format(now);//把时间类型改为字符串    
  89.         File fileoutput = new File("E:\\wl121.txt"+"\\考勤结果" + strDate);    
  90.         PrintWriter output = null;//准备输出考勤结果    
  91.         try  
  92.         {    
  93.             output = new PrintWriter(fileoutput);//构造新的PrintWriter,指定fileoutput为其生成值    
  94.         }   
  95.         catch (FileNotFoundException e1)   
  96.         {    
  97.         }  
  98.         if(count2 == count)    
  99.         {    
  100.             output.print("缺课名单: ");    
  101.             output.print(strAbsent);//输出考勤结果    
  102.             output.close();    
  103.         }    
  104.     }    
  105. }           

 


0 0
原创粉丝点击