1.关于方法的使用
public class Student {
private String number;
private String name;
private int age;
public Student() { //无参构造方法
}
public String getNumber() { //访问器
return number;
}
public void setNumber(String number) { //修改器
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String number, String name, int age) { //全参构造方法
super();
this.number = number;
this.name = name;
this.age = age;
}
}
2.把串“12hello345”中的数字替换为你好:
public class ti01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="12hello345";
s=s.replaceAll("\\d","你好");//替换
System.out.println(s);
}
}
3.提取串“1931年3月5日”中的年月日数值
public class ti01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="1931年3月5日";
String [] time =str.split("\\D");
System.out.println(time[0]);
System.out.println(time[1]);
System.out.println(time[2]);
}
}
4.执行下列代码后输出:
String str1 = "jdk";
String str = str1.substring(1);
System.out.println(str);
答案: dk
5.下列代码的输出结果是:
String str1 = "Hello world!";
String str2 = "IncludeHelp";
if(str1.startsWith("Hello")){
System.out.println(str1 + " starts with Hello" );
}
else{
System.out.println(str1 + " does not start with Hello" );
}
if(str2.startsWith("inc")){
System.out.println(str2 + " starts with inc" );
}
else{
System.out.println(str2 + " does not start with inc" );
}
答案: Hello world! starts with Hello IncludeHelp does not start with inc
6.写一个正则表达式,要求能匹配 {babble, bebble, bibble, bobble, bubble}
答案: b[aeiou]bble;b[aeiou]b{2}le
7.写一个正则表达式,要求能匹配 {ggle, gogle, google, gooogle, goooogle, ...}
答案: go*gle;g[o]*gle
8.随机数
public class xiaol01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random r1=new Random();
int k=r1.nextInt(50);
System.out.println(k);
Random r2=new Random(5);
int X=r2.nextInt(50);
System.out.println(X);
}
}