【1】
1 public class Java401 { 2 public static void main(String[] args) { 3 String str1="Java技术学习班 20070326"; 4 String str2="Java技术学习班 20070326 MLDN 老师"; 5 String str3="330521199601221711"; 6 System.out.println(str1.substring(10,18)); 7 System.out.println(str1.charAt(7)); 8 System.out.println(str1.replaceAll("0", "")); 9 System.out.println(str2.replaceAll(" ", "")); 10 System.out.println(str3.substring(6,14)); 11 String str="MLDN JAVA"; 12 String newStr=str.replaceAll("JAVA", "J2EE"); 13 System.out.println("替换之后的结果:"+newStr); 14 } 15 }
程序运行结果
【2】
1 class Book{ 2 private String name; 3 private int id; 4 private float price; 5 private static int num=12; 6 private static int count=0; 7 public Book(){ 8 count++; 9 this.id=count; 10 } 11 public Book(int id){ 12 this.id=id; 13 } 14 public int getId(){ 15 return this.id; 16 } 17 public Book(String name,float price){ 18 this.name=name; 19 this.price=price; 20 this.num=num; 21 } 22 public void setName(String name){ 23 this.name=name; 24 } 25 public String getName(){ 26 return this.name; 27 } 28 public void setPrice(float price){ 29 this.price=price; 30 } 31 public float getPrice(){ 32 return this.price; 33 } 34 public void setNum(int num){ 35 this.num=num; 36 } 37 public int getNum(){ 38 return this.num; 39 } 40 } 41 public class Java402 { 42 public static void main(String[] args) { 43 Book book[]=new Book[6]; 44 book[0]=new Book("解忧杂货店",36.9f); 45 book[1]=new Book("名侦探的秘密 ",26.6f); 46 book[2]=new Book("白夜行 ",67.9f); 47 book[3]=new Book("流星之绊 ",52.5f); 48 book[4]=new Book("名侦探的守则 ",62.3f); 49 book[5]=new Book("彷徨之刃 ",13f); 50 for(int i=0;i<book.length;i++){ 51 System.out.println("编号:"+new Book().getId() 52 +"\t 书名:"+book[i].getName() 53 +"\t 价格:"+book[i].getPrice() 54 +"\t 数量:"+book[i].getNum()); 55 } 56 System.out.println("图书总量为:"+(new Book().getId()-1)*book[1].getNum()); 57 } 58 }
程序运行结果