输出格式的对齐
1.\t
的使用
- 一般在输出时可能会出现需要格式对齐情况,例如以下的日历输出(已经调整过)
- 但再这之前使用了制表符
\t
来对齐, \t
用法
1 .\t
表示制表符,相当于制表符
2.前面的输出内容位数为8的倍数,\t
将输出8个空格
3.前面的输出内容位数不是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;
cl.set(Calendar.YEAR, restYear);
cl.set(Calendar.MONTH, restMonth-1);
cl.set(Calendar.DAY_OF_MONTH, restDay);
long time1 = cl.getTimeInMillis();
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);
if((int)betweenDays%4 == 0){
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
容易出现这种情况)
2.String.format
- 于是想到将输出内容全部转为字符串,再调用String.format来标齐
String.format("%-8s","["+ i +"]")
- 这样更方便自己调节格式的长度
- 字符串使用
%s
关注左对齐和右对齐即可
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;
cl.set(Calendar.YEAR, restYear);
cl.set(Calendar.MONTH, restMonth-1);
cl.set(Calendar.DAY_OF_MONTH, restDay);
long time1 = cl.getTimeInMillis();
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);
if((int)betweenDays%4 == 0){
if(cl.get(Calendar.DAY_OF_WEEK) == 1 || cl.get(Calendar.DAY_OF_WEEK) == 7){
restWeekNum++;
}
restNum++;
if(count == 7){
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();
}
}