1.计算闰年:判断某一年份是否为闰年。(如果这个年份能被4整除,但不能被100整除;或者,如果这个年份能被4整除,又能被400整除;满足以上两个条件中的一个的年份为闰年)。类名:LeapYear
public class LeapYear {
public static void main(String args[])
{
int i=400;
if((i%4==0&&i%100!=0)||(i%4==0&&i%400==0))
System.out.println(i+"年是闰年");
else
System.out.println(i+"年是不闰年");
}
}
其运行结果如下图
2:在签到程序中过滤掉自己从而不被点名
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
@SuppressWarnings("unused")
public class Registre1 {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//(a)使用命令行参数,输入学生名单,和 班级名称
// 使用格式: java RegisterApp list.txt wl121
if(args.length != 2){
System.out.println("参数输入不对");
System.out.println("使用方法(示例):java RegisterApp 名单文件名称 班级名称");
System.exit(0);
}
//(b)学生签到结果:学生到,输入1;缺课,输入0
System.out.println("——————————————————");
System.out.println("简易学生签到程序V0.1");
System.out.println("老师叫到名字,请答‘到’");
System.out.println("1:到课 0:缺课");
System.out.println("——————————————————");
//(c)取得系统当前日期时间
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHMM");//可以方便地修改日期格式
String strDate = dateFormat.format( now );
System.out.println("当前时间:"+strDate);
//(d)读取学生名单,args[0]为学生名单文件,args[1]为班级名称
String fileList = args[0];
String fileCheck = args[1] + strDate + ".txt";
File fileInput = new File(fileList);
File fileOutput = new File(fileCheck);
//(e)利用Scanner类读取文本数据/键盘输入数据; PrintWriter类把签到结果写入到文件
Scanner input = new Scanner(fileInput);
Scanner sc = new Scanner(System.in);
PrintWriter output = new PrintWriter(fileOutput);
//保存缺课学生名字strAbsent ,缺课学生人数nAbsent, 是否缺课标记flag
String strAbsent = "";
int nAbsent = 0;
int flag = 0;
while(input.hasNext()){ //循环读取学生数据
String strName=input.nextLine();
//把学生名字输出到屏幕,从而进行点名。
//老师根据学生到课情况,输入1-到课,0-缺课,保存到flag中
if(strName.equals("7 XXX")){ //过滤掉自己,使自己不被点名
flag=1;
}
else
{
System.out.println(strName);
flag = sc.nextInt();
}
//如果缺课,则记录下缺课学生数目 与 名字
if(flag==0){
nAbsent = nAbsent+1;
strAbsent = strAbsent + " " + strName;
}
//把考勤结果写入名单
output.print(strName);
output.print(" ");
output.println(flag);
}
//关闭I/O管道
sc.close();
output.close();
input.close();
System.out.println("——————————————————————————");
System.out.println("考勤结束.");
System.out.printf("一共有%d个同学缺课,分别是:%s\n",nAbsent,strAbsent);
System.out.println("——————————————————————————");
}
}
但是对于下面这些添加了按钮功能的签到程序就遇到一些问题
import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import javax.swing.*;
public class Registre {
static int count2 = 0;
static int count = 0;
static String namel[][];
static JLabel nameText =new JLabel();
static String inname,outname;
static int aAbsent=0;
static JLabel outlab =new JLabel();
public static void main(String[] args) throws FileNotFoundException {
//(a)运用图形界面知识,做出简单界面
JFrame frame = new JFrame("课堂签到程序");
JLabel nameLab =new JLabel("请输入名字:");
JButton yes =new JButton("到");
JButton no =new JButton("缺席");
JButton end =new JButton("统计");
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
pan1.add(nameLab);
pan1.add(nameText);
pan2.add(yes);
pan2.add(no);
pan2.add(end);
frame.setLayout(new BorderLayout(3,3));//使用BorderLayout布局
frame.add(pan1,BorderLayout.NORTH);
frame.add(pan2,BorderLayout.CENTER);
frame.add(outlab,BorderLayout.SOUTH);
frame.pack();
frame.setSize(290,150);
frame.setVisible(true);
/*(b)根据老师给出的签到程序RegisterApp.java,加以利用,使用命令行参数加载名单到nameText的标签上,
利用数组 存储学生学号,名字和输入流 Scanner类等知识 */
if (args.length != 2) {
System.out.println("参数输入不对");
System.out.println("使用方法(示例):java RegisterApp 名单文件名称 班级名称");
System.exit(0);
}
inname = args[0];
outname = args[1];
File filein = new File(inname);
Scanner fin = new Scanner(filein);
Scanner fin1 = new Scanner(filein);
while (fin.hasNext()) {
count++;
fin.nextLine();
if(fin.equals("7 XXX"))continue;
//{ //过滤掉自己,使自己不被点名
// int flag = 1;
//}
}
fin.close();
namel = new String[count][2];
for (int a = 0; a < count; a++) {
namel[a][0] = fin1.nextLine();
}
fin1.close();
nameText.setText(namel[count2][0]);
//(c)通过按钮事件,读取名单,老师可以开始点名,最终统计把结果记录以文档输出
yes.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("到") && count2 < count)
{
nameText.setText(namel[count2][0]);
namel[count2][1] = "1";
count2++;
}
}
});
no.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("缺席") && count2 < count)
{
nameText.setText(namel[count2][0]);
namel[count2][1] = "0";
count2++;
aAbsent++;
}
}
});
end.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("统计") && count2 == count) {
//System.out.println(000000);
File fileout = new File(outname+"--"+00+ ".txt");
try {
PrintWriter fout = new PrintWriter(fileout);
for (int a = 0; a < count; a++)
{
String s = namel[a][0] + " " + namel[a][1];
fout.println(s);
}
fout.close();
outlab.setText("考勤结束:"+"该班共有"+count+"人"+"一共有"+aAbsent+"个同学缺席");
}
catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
}
}
});
}
}
对原有的程序做了一些修改,例如if(fin.equals("7 XXX"))continue;可是其运行结果还是没有过滤掉自己的名字