Java - 尚学堂第八章常用类(将输入的string类型的值转为整数、浮点型、日期类型)

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner; public class TestASE {
public static void main(String[] args) throws ParseException {
System.out.println("请输入年龄:");
Scanner scanner = new Scanner(System.in);
String age = scanner.nextLine();
System.out.println("请输入分数:");
String score = scanner.nextLine();
System.out.println("请输入入学日期(格式为2020-10-20):");
String date = scanner.nextLine(); //转换为整数、浮点型、日期类型
int age2 = Integer.parseInt(age);
double score2 = Double.parseDouble(score);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = df.parse(date);//先转为时间类型
Calendar c = new GregorianCalendar();
c.setTime(date2); System.out.println("年龄" + age2 + "岁\n"
+ "分数:" + score2 + "分\n"
+ "入学时间" + date2);
}
}

转自https://blog.csdn.net/xxyh1234/article/details/89495876

上一篇:Visual Studio Code配置GoLang开发环境


下一篇:VS 2010配置Qt 4.8.5