今晚的月色温柔

导航

输入工具类设计

 

 

 

【输入形式】阅读main方法,了解数据的输入和输出需求。
【输出形式】
【样例输入】
【样例输出】
【样例说明】日期类型数据输入格式为:2020-11-20,输出为2020/11/20

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

class InputUtils {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static String getString() {
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return line;
}

public static int getInt() {
String line;
boolean flag = true;
int result = 0;
while (flag) {
line = getString();
if (line.matches("\\d+")) {
result = Integer.parseInt(line);
flag = false;
} else {
System.err.println("请输入整数!");
}
}
return result;
}

public static double getDouble() {
String line;
boolean flag = true;
double result = 0;
while (flag) {
line = getString();
if (line.matches("\\d+.?\\d+")) {
result = Double.parseDouble(line);
flag = false;
} else {
System.err.println("请输入整数!");
}
}
return result;
}

public static Date getDate() {
String line;
Date d = null;
boolean flag = true;
while (flag) {
line = getString();
if (line.matches("\\d{4}-\\d{2}-\\d{2}")) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
d = sdf.parse(line);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
flag = false;
} else {
System.err.println("输入日期(年-月-日)");
}
}
return d;
}
}


public class Main {

public static void main(String[] args) {
int selected;
selected = InputUtils.getInt();
switch (selected) {
case 1:
System.out.println(InputUtils.getString().length());
break;
case 2:
int a=InputUtils.getInt();
System.out.println(a);
break;
case 3:
double b=InputUtils.getDouble();
System.out.println(b);
break;
case 4:
Date date=InputUtils.getDate();
System.out.println(new SimpleDateFormat("yyyy/MM/dd").format(date));
case 5:

}
}
}

posted on 2020-12-12 16:57  1022  阅读(147)  评论(0编辑  收藏  举报