1.定义一个方法,根据用户身份证号码计算其年龄并输出。
public class John {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();//创建集合对象list
Random r = new Random();//创建Random类对象r
for (int i = 0; i < 10; i++) {
int x = r.nextInt(100) + 1;//范围为 1-100
list.add(x);
}
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.print(it.next()+" ");
}
}
2.采用 Random 类或者 Math 类随机生成一组随机数,存入 List 集合类( ArrayListLinkedList 等都可以),并利用 Iterator 接口遍历集合
public class John {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();//创建集合对象list
Random r = new Random();//创建Random类对象r
for (int i = 0; i < 10; i++) {
int x = r.nextInt(100) + 1;//范围为 1-100
list.add(x);
}
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.print(it.next()+" ");
}
}
3.获取当前的系统日期和时间来产生订单的单号,单号的构成: D +年份+月份+日期+时钟+分钟+秒+系统用户账号,要用到日期类 Date (通过使用 API文档来掌握该类的用法)
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class John {
public static void main(String[] args) {
String text = "D";
StringBuffer str = new StringBuffer(text);
Date date = new Date();//获取系统当前的时间
//SimpleDateFormat 用于把时间转化为指定的格式
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
// df.format(date) 用于将指定的时间格式转换为字符串
// System.getProperty("user.name") 用于获取当前的用户账号
str.append(df.format(date)).append( System.getProperty("user.name"));
System.out.println(str);
}
}
```![在这里插入图片描述](https://img-blog.csdnimg.cn/7ffd88cf207348b1801f7a7d325711b2.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p6r5rid5rWq5aSp5LiL,size_20,color_FFFFFF,t_70,g_se,x_16)
**4.编写一个程序,实现字符串大小写的转换并倒序输出。具体要求:(1)使用 for 循环将字符串“ ITcastHeiMa ”从最后一个字符开始遍历。(2)遍历的当前字符如果是大写字符,就使用 toLower Case ()方法将其转换为小写字符,反之则使用 toUpper Case ()方法将其转换为大写字符。(3)定义一个 StringBuffer 对象,调用 append ()方法依次添加遍历的字符,最后调用 StringBuffer 对象的 toString ()方法,并将得到的结果输出。**
```java
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class John {
public static void main(String[] args) {
String str = "ITcastHeiMa";
// 设一个可拼接字符串
StringBuffer buffer = new StringBuffer();
for (int i = str.length()-1;i>=0;i--) {
char c = str.charAt(i);//拿到字符串里的每个字符
if(c >= 'A' && c<= 'Z'){
// 首先将字符转换为字符串,然后使用toLowerCase()方法,最后再用append拼接返回的字符串
buffer.append(String.valueOf(c).toLowerCase()) ;
}else{
buffer.append(String.valueOf(c).toUpperCase()) ;
}
}
System.out.println(buffer);
}
}