\t
的使用\t
来对齐,\t
用法 \t
表示制表符,相当于制表符 \t
将输出8个空格 \t
将补足8位System.out.print("["+ i +"]\t\t");
System.out.println(i + "\t\t");
import java.util.Calendar;
import java.util.Scanner;
/**
*找到休息日(常用类库)
*/
public class Task_010301_002 {
/**
* 创建一个日历对象
*/
private static Calendar cl = Calendar.getInstance();
private static Scanner input = new Scanner(System.in);
/**
* 测试
*/
public static void main(String[] args) {
new Task_010301_002().view1();
}
/**
* 页面展示
*/
public void view1(){
System.out.println("请输入年:");
String text = input.nextLine();
int year = 0;
try{
year = Integer.parseInt(text);
}catch(NumberFormatException e){
System.err.println("输入格式不正确!");
view1();
}
System.out.println("请输入月");
text = input.nextLine();
int month = 0;
try{
month = Integer.parseInt(text);
}catch(NumberFormatException e){
System.err.println("输入格式不正确!");
view1();
}
view2(year, month);
}
public void view2(int year, int month){
int restYear = 2019;
int restMonth = 2;
int restDay = 2;
//设置Calendar并获取当时的时间戳
cl.set(Calendar.YEAR, restYear);
cl.set(Calendar.MONTH, restMonth-1);
cl.set(Calendar.DAY_OF_MONTH, restDay);
//获取第一个休息日的时间戳
long time1 = cl.getTimeInMillis();
//设置Calendar为用户输入的年月
cl.set(Calendar.YEAR, year);
cl.set(Calendar.MONTH, month-1);
cl.set(Calendar.DAY_OF_MONTH,1);
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六" );
//获取第一天是星期几
int firstDay = cl.get(Calendar.DAY_OF_WEEK);
//获取当前年的月份的总天数
int dayOfMonth = cl.getActualMaximum(Calendar.DAY_OF_MONTH);
int count = firstDay;
//记录本月休息天数
int restNum = 0;
//记录周末休息天数
int restWeekNum = 0;
//用于输出制表符以填充每月第一天前的空位
for(int i = 1;i < firstDay; i++){
System.out.print("\t\t");
}
//循环输出每一日
for(int i = 1; i <= dayOfMonth; i++){
cl.set(Calendar.DAY_OF_MONTH, i);
//获取用户设定年月中的每一天的时间戳
long time2 = cl.getTimeInMillis();
//计算当前设定日期和第一个休息日的天数差
long betweenDays=(time2-time1)/(1000*3600*24);
//判断是否为休日,count = 7则换一次行
if((int)betweenDays%4 == 0){
//如果是周末则restWeekNum加1
if(cl.get(Calendar.DAY_OF_WEEK) == 1 || cl.get(Calendar.DAY_OF_WEEK) == 7){
restWeekNum++;
}
restNum++;
if(count == 7){
//在这
System.out.println("["+ i +"]\t\t");
count = 1;
}else{
System.out.print("["+ i +"]\t\t");
count++;
}
}else{
if(count == 7){
//在这
System.out.println(i + "\t\t");
count = 1;
}else{
System.out.print(i + "\t\t");
count++;
}
}
}
System.out.println();
System.out.println("本月休息天数有:" + restNum);
System.out.println("本月轮到周末休息天数是" + restWeekNum + "天");
view1();
}
\t
容易出现这种情况) String.format("%-8s","["+ i +"]")
%s
关注左对齐和右对齐即可 import java.util.Calendar;
import java.util.Scanner;
/**
* 找到休息日(常用类库)
*
* 某公司软件开发工程师孙工,作息规律为上三天班,休息一天,经常不确定休息日
*是否周末,为此,请你开发一个程序,当孙工输入年及月,以日历方式显示对应月
*份的休息日,用中括号进行标记.同时,统计出本月有几天休息,轮到周末休息有
*几天.(注:首次休息日是 2019 年 2 月 2 日)
*/
public class Task_010301_002 {
/**
* 创建一个日历对象
*/
private static Calendar cl = Calendar.getInstance();
private static Scanner input = new Scanner(System.in);
/**
* 测试
*/
public static void main(String[] args) {
new Task_010301_002().view1();
}
/**
* 页面展示
*/
public void view1(){
System.out.println("请输入年:");
String text = input.nextLine();
int year = 0;
try{
year = Integer.parseInt(text);
}catch(NumberFormatException e){
System.err.println("输入格式不正确!");
view1();
}
System.out.println("请输入月");
text = input.nextLine();
int month = 0;
try{
month = Integer.parseInt(text);
}catch(NumberFormatException e){
System.err.println("输入格式不正确!");
view1();
}
view2(year, month);
}
public void view2(int year, int month){
int restYear = 2019;
int restMonth = 2;
int restDay = 2;
//设置Calendar并获取当时的时间戳
cl.set(Calendar.YEAR, restYear);
cl.set(Calendar.MONTH, restMonth-1);
cl.set(Calendar.DAY_OF_MONTH, restDay);
//获取第一个休息日的时间戳
long time1 = cl.getTimeInMillis();
//设置Calendar为用户输入的年月
cl.set(Calendar.YEAR, year);
cl.set(Calendar.MONTH, month-1);
cl.set(Calendar.DAY_OF_MONTH,1);
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六" );
//获取第一天是星期几
int firstDay = cl.get(Calendar.DAY_OF_WEEK);
//获取当前年的月份的总天数
int dayOfMonth = cl.getActualMaximum(Calendar.DAY_OF_MONTH);
int count = firstDay;
//记录本月休息天数
int restNum = 0;
//记录周末休息天数
int restWeekNum = 0;
//用于输出制表符以填充每月第一天前的空位
for(int i = 1;i < firstDay; i++){
System.out.print("\t\t");
}
//循环输出每一日
for(int i = 1; i <= dayOfMonth; i++){
cl.set(Calendar.DAY_OF_MONTH, i);
//获取用户设定年月中的每一天的时间戳
long time2 = cl.getTimeInMillis();
//计算当前设定日期和第一个休息日的天数差
long betweenDays=(time2-time1)/(1000*3600*24);
//判断是否为休日,count = 7则换一次行
if((int)betweenDays%4 == 0){
//如果是周末则restWeekNum加1
if(cl.get(Calendar.DAY_OF_WEEK) == 1 || cl.get(Calendar.DAY_OF_WEEK) == 7){
restWeekNum++;
}
restNum++;
if(count == 7){
//用String.format进行操作使其对齐
System.out.println(String.format("%-8s","["+ i +"]"));
count = 1;
}else{
System.out.print((String.format("%-8s", "[" + i + "]")));
count++;
}
}else{
if(count == 7){
System.out.println(String.format("%-8s", Integer.toString(i)));
count = 1;
}else{
System.out.print(String.format("%-8s", Integer.toString(i)));
count++;
}
}
}
System.out.println();
System.out.println("本月休息天数有:" + restNum);
System.out.println("本月轮到周末休息天数是" + restWeekNum + "天");
view1();
}
}
原网址: 访问
创建于: 2023-09-21 09:41:39
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论