1、【编号:1301】现获取到两个字符串格式的数字:字符串23和字符串24。请编写程序,计算两个数字的和,并输出在控制台。 运行结果: 相加结果为:47
public static void main(String[] args) { String num = "23"; String num1 = "24"; int res = Integer.valueOf(num)+Integer.valueOf(num1); System.out.println("相加结果为:"+res); }2、【编号:1302】现获取到用户填写的3个年龄信息,但格式是以逗号分隔的字符串形式,例如:“23,24,25”。 现需要求出这三个年龄的平均年龄,请编写程序实现,并将结果打印到控制台。 运行结果: 平均年龄为:24
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入三个年龄信息,以逗号隔开"); String st = sc.next(); String [] arr = st.split(","); int res = 0; for (int i = 0; i < arr.length; i++) { res += Integer.valueOf(arr[i]); } System.out.println(res/arr.length); }3、【编号:1303】请编写程序,获取当前系统日期,并将结果打印到控制台,要求展示格式为:xxxx年xx月xx日 运行结果: 2088年2月15日
public static void main(String[] args) { Date d = new Date(); SimpleDateFormat date = new SimpleDateFormat("yyyy年MM月dd日 "); String s = date.format(d); System.out.println(s); }4、【编号:1304】请将字符串"2008-08-08"字符串,转换为日期对象,并将转换后的对象,以及毫秒值输出在控制台 public class Demo3 { public static void main(String[] args) { String s = “2008-08-08”;
/* ----- 将字符串s转换为日期对象,并得到毫秒值.----- */} } 运行结果: Fri Aug 08 00:00:00 CST 2008 毫秒值为:1218124800000
public static void main(String[] args) throws ParseException { String s = "2008-08-08"; SimpleDateFormat da = new SimpleDateFormat("yyyy-MM-dd"); Date res = da.parse(s); Long time = res.getTime(); System.out.println(res); System.out.println("毫秒值为:"+time); }5、【编号:1305】某公司想统计下员工截至到当前时间,入职公司的天数。请编写程序实现该需求,由键盘录入员工的入职日期, 格式为:xxxx年xx月xx日。将员工入职公司的天数打印到控制台。 运行结果(假设当前时间是2018年12月10日): 请输入您入职的日期,格式为:xxxx年xx月xx日 2018年12月09日 您入职公司已经1天
public static void main(String[] args) throws ParseException { Scanner sc = new Scanner(System.in); System.out.println("请输入您入职的日期,格式为:xxxx年xx月xx日"); String time = sc.next(); Calendar a = Calendar.getInstance(); a.set(2018,11,10); SimpleDateFormat da = new SimpleDateFormat("yyyy年MM月dd日"); Date d1 = da.parse(time); long l = d1.getTime(); long ll =a.getTimeInMillis(); long res = ll-l; System.out.println("您入职公司已经"+(res/1000/60/60/24)+"天"); }6、【编号:1308】某某研发团队在2018年2月1号宣布,距离项目发版上线还剩100天,在这个时限内, 我们务必要将项目搞定!但100天以后到底是几月几号啊,我们总不能翻日历一天一天查吧,请使用技术手段,解决这一问题 运行结果: 100天后的时间为:2018年5月12日
public static void main(String[] args) { Calendar a = Calendar.getInstance(); a.set(2018,1,1); a.add(Calendar.DATE,100); int year = a.get(Calendar.YEAR); int march = a.get(Calendar.MARCH)+1; int date = a.get(Calendar.DATE); System.out.println("100天后的时间为:"+year+"年"+march+"月"+date+"日"); }7、【编号:1309】请在补齐代码,将try语句中可能出现问题得异常对象,全部进行捕获,捕获的顺序不限制, 最终要求控制台输出 【捕获数组索引越界异常】 public class Demo2 { /* -----请编写代码,将可能会出现的异常全部进行捕获----- */ public static void main(String[] args) { try{ String[] arr = {“abc”,“bbc”,“ccc”}; System.out.println(arr[10]); arr = null; System.out.println(arr[0]); }catch (){
}catch (){ } }} 运行结果: 捕获数组索引越界异常
public static void main(String[] args) { try{ String[] arr = {"abc","bbc","ccc"}; System.out.println(arr[10]); arr = null; System.out.println(arr[0]); }catch (ArrayIndexOutOfBoundsException e){ System.out.println("捕获数组索引越界异常"); }catch (NullPointerException e){ System.out.println("捕获数组空指针异常"); } }8、【编号:1310】请对如下代码进行异常处理,要求不会影响到后续代码的继续执行,如果程序出现错误,控制台展示出现的异常名称、 异常的原因、异常出现的位置。 public class Demo3 { public static void main(String[] args) { /* -----请对异常进行处理,要求不会影响到后续代码的继续执行----- -----如果程序出现错误,控制台展示出现的异常名称、异常的原因、异常出现的位置----- */ System.out.println(10 / 0); System.out.println(“我是程序的后续代码”); } }
public static void main(String[] args) { try { System.out.println(10 / 0); } catch (ArithmeticException e) { e.printStackTrace(); } System.out.println("我是程序的后续代码"); }9、【编号:1312】某程序员编写了一个方法,该方法实现的逻辑是从传入的数组中找出最大值,但考虑到调用者可能会传入一个长度为0的数组, 所以要给调用者进行相应的提示,但是Java中并没有合适的异常类名可以直观的让调用者明白出现的问题, 该程序员想自己设计一个运行时异常ArrayLengthNotZeroException,为自己的方法提供服务,请帮其实现功能
public class ArrayLengthNotZeroException extends RuntimeException { public ArrayLengthNotZeroException() { } public ArrayLengthNotZeroException(String message) { super(message); } } public static void main(String[] args) { int[] arr = new int[0]; int max = getMax(arr); System.out.println("最大值为:" + max); } public static int getMax(int[] arr){ if(arr.length == 0){ // -----抛出ArrayLengthNotZeroException运行时异常----- throw new ArrayLengthNotZeroException("数组长度不能为0"); } int max = arr[0]; for (int i = 0; i < arr.length; i++) { if(max < arr[i]){ max = arr[i]; } } return max; }