62,615
社区成员
发帖
与我相关
我的任务
分享
package Char12;
import java.io.*;
import java.text.*;
import java.util.*;
class InputData{
private BufferedReader buf = null;
public int count =1;
public InputData(){
this.buf = new BufferedReader(new InputStreamReader(System.in));
}
public String getInfo(){
String temp = null;
if(this.count>1){
this.count =1;
System.out.print("请你输入第"+this.count+++"个日期:");
}else{
System.out.print("请你输入第"+this.count+++"个数字:");
}
try {
temp = this.buf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return temp;
}
public int getInt(){
int temp = 0;
String str = null;
boolean flag = true;
while(flag){
str = this.getInfo();
if(str.matches("^\\d+{1}quot;)){
temp = Integer.parseInt(str);
flag = false;
}else{
System.out.println("输入的数据必须是数据,请重新输入!");
this.count = count-1;
}
}
return temp;
}
public Date getDate(){
Date d = null;
boolean flag = true;
String str = null;
while(flag){
str = this.getInfo();
if(str.matches("^\\d{4}-\\d{2}-\\d{2}{1}quot;)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
d = sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
flag = false;
}else{
System.out.println("输入的数据必须是日期,请重新输入!");
}
}
return d;
}
};
public class BufferedReaderDemo02 {
public static void main(String args[]){
int i = 0 ;
int j = 0 ;
InputData input = new InputData();
i = input.getInt();
j = input.getInt();
System.out.println(i+"+"+j+"="+(i+j));
Date d = null;
d = input.getDate();
DateFormat sdf = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","Cn"));
System.out.println(sdf.format(d));
}
}
import java.util.*;
public class Test {
public static void main(String[] args) throws Throwable {
Scanner sc = new Scanner(System.in);
String buf;
int year;
GregorianCalendar c = new GregorianCalendar();
while (true) {
System.out.print("请输入年([yyyy]格式):");
buf = sc.nextLine();
try {
year = Integer.parseInt(buf);
if (year < 0) {throw new Exception("负数");}
System.out.println(c.isLeapYear(year) ? "闰年" : "不是闰年");
System.out.print("是否继续?(y/n):");
buf = sc.nextLine();
if ("y".equalsIgnoreCase(buf)) break;
} catch (Exception e) {
System.out.println("输入错误,请重输");
}
}
}
}