java高级开发 第九章——常用实用类 练习题

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);
            }

        }

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值