1. 根据输入的日期(年月日),输出这一年的第几天
题目描述:根据输入的日期,计算是这一年的第几天
。
解题思路:
step 1:判断输入日期的合法性,如果输入不合法返回-1;
step 2:根据输入的月份,计算从1月到(month - 1)月的天数,如果是二月的话,就要判断该年是否为闰年(闰年,day加29天,反之,day加28天);
step 3:根据步骤二计算出来的数值,最后加上输入的date,输出即可。
详细代码:
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int year = sc.nextInt();
int month=sc.nextInt();
int date=sc.nextInt();
int day=0;
if(year<=0 || month>12 || month<=0 || date>31 || date<=0){
System.out.println(-1);
return;
}
for (int i = 1; i < month; i++) {
if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12){
day+=31;
}else if(i==4 || i==6 || i==9 || i==11){
day+=30;
//能被400整除,或者能被4整除但不能被100整除的都是闰年
}else if(i==2 && (year%400==0 || (year%4==0 && year%100!=0) )){
day+=29;
}else{
day+=28;
}
}
day+=date;
System.out.println("总天数为:"+day);
}
}
2. 开启5个线程卖100张票,每隔一秒钟卖一张票
public class TicketSales {
/*
* 1、创建线程数量为5的线程池
* 2、同时运行5个买票窗口
* 3、总票数为100,每隔一秒钟卖一张票
*/
static int tickets=100;
static String str="";
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(5);
service.execute(new Runnable() {
public void run() {
while(tickets>0){
synchronized (str){
if(tickets>0){
System.out.println(Thread.currentThread().getName()+"线程任务开始卖票,卖票1张,剩余"+(--tickets)+"张");
try {
Thread.sleep(1000);//休眠一秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
});
service.shutdown();
}
}
3. 遍历文件夹下的所有文件,并输出文件夹下所有的文件名
public class IO {
public static void main(String[] args) {
//遍历某文件夹下的文件输出文件名
File file=new File("C:\\Users\\28486\\Desktop\\test\\");
File[] files = file.listFiles();
if(files.length>0){
for (File f : files) {
System.out.println(f.getName());
}
}
}
}