Java Cheat List

1.控制台读入字符串

String pair ="";
try
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    pair = br.readLine(); 
}
catch(IOException ex)
{
    ex.printStackTrace();
}

2.控制台读入整数

int a=0;
try{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    a=Integer.parseInt(br.readLine());
    b=Integer.parseInt(br.readLine());
    c=Integer.parseInt(br.readLine());
}catch(IOException ex){
    ex.printStackTrace();
}

3.计算某年某月为星期几

int y=2000,m=1,d=1;
Calendar aCalendar=Calendar.getInstance();
aCalendar.set(Calendar.YEAR,y);
aCalendar.set(Calendar.MONTH, m-1);
aCalendar.set(Calendar.DAY_OF_MONTH, d);
//注意,x为1-7,默认1为星期日
int x=aCalendar.get(Calendar.DAY_OF_WEEK);

Comments are closed.