千锋Java&Day22课后作业

努力到无能为力,拼搏到感动自己!
加油!

总结

内部类:
1)在一个类的内部再定义一个完整的类
2)成员内部类,静态内部类,局部内部类,匿名内部类

Object类:
所有类的直接或间接父类,可存储任何对象。

包装类:
基本数据类型所对应的引用数据类型,可以使Object统一所有数据。

String类:
字符串是常量,创建之后不可改变,字面值保存在字符串池中,可以共享。

BidDecimal:
可精确计算浮点数。

课后作业

7.从命令行上读入一个字符串,用两种不同的方法,把该字符串转换为一个 int 类型

方法一:把 String 直接转换为 int
方法二:把 String 转换为 Integer,再把 Integer 转换为 int 类型
 import java.util.Scanner;
       public class StringToInt {     
            public static void main(String[] args) {        
            String str = sc.nextLine();        
             //方法一,,string --> int        
             int i1 = Integer.parseInt(str);        
                System.out.println(i1);         

             //方法二,string --> Integer --> int        
             Integer ii = new Integer(str);       
             int i2 = ii.intValue();        
                 System.out.println(i2);     
            }
       }       

8.(toString,字符串加法)有下面代码

class Student{
        private int age; 
        private String name; 
        public Student(){}
        
        public Student(String name, int age){ 
           this.name = name;
           this.age = age;
         }
         public String toString(){ 
            return name + “ ” + age;
         }
}
      public class TestStudent{
            public static void main(String args[]){ 
             Student stu1 = new Student(“tom”, 18); 
             System.out.println(/*1*/);
             }
      }
问:在/*1*/位置,填入什么代码能编译通过?
    A. stu1 + “ ” + 100
    B. 100 + “ ” + stu1
    C.“ ” + 100 + stu1
    D.stu1 + 100 + “ ”
A
B
C 

11.获取邮箱“zhengcg@zparkhr.com”中的用户名”zhengcg”。

public class Test1 {
    public static void main(String[] args) {      
          String strings = "zhengcg@zpargkm.com";
          System.out.println(strings);     
          String[] s = strings.split("@");      
          System.out.println(s[0]);    
     }
}

12.验证邮箱“zhengcg@zparkhr.com”是否是一个合法的邮箱格式。

提示:
1)邮箱必须包含“@”和“ .2)最后一个“ . ”的位置必须大于“@”的位置。
public class Test2 {
    public static void main(String[] args) {        
         String s = "zhengcg@zpargkm.com";        
         int check1 = s.indexOf("@");     
         int check2 = s.indexOf(".");        
         if (check1 != -1 && check2 != -1 && check1 < check2){ 
                 System.out.println("此邮箱合法。");        
            }else{           
                 System.out.println("邮箱不合法!");        
                 }  
    }
}

13.将随机获取的 UUID(含义是通用唯一识别码 Universally Unique Identifier)中的”- ”去掉。

提示:
1)java.util.UUID.randomUUID().toString() //可以获取随机 UUID
2)格式:e6c57443-1667-4d75-98f6-a8863d95e58f
public class Test3 {
      public static void main(String[] args) {        
             String s = java.util.UUID.randomUUID().toString();
             System.out.println(s);    
              System.out.println(s.replaceAll("-",""));    
       }
}

14.在给定的字符串“ABCDEFGhijklmn1234567”中获取随机的 4 个字符,并使用 StringBuilder 拼接成字符串。(随机获取到的 4 个字符中可以出现重复字符)

提示:创建随机数对象 java.util.Random。 java.util.Random random = new java.util.Random(); random.nextInt(100); //可以获取到 0~99 中任意一个随机数
public class Test4 {
	public static void main(String[] args) {	
		String s="ABCDEFGhijklmn1234567";		
		Random random=new Random();		
		StringBuilder c=new StringBuilder();		
		for(int i=0;i<4;i++) {
	 	c=c.append(s.charAt(random.nextInt(s.length())));	
	 		}		
	 		System.out.println(c);	
	   }
}

15.给定一个由数字组成的字符串
如:“1239586838923173478943890234092”;统计出每个数字出现的次数。

public class Test5 {
     public static void main(String[] args) {        
          String s = "1239586838923173478943890234092";     
            char[] c = s.toCharArray();       
           for (int i = 0;i <= 9;i++){           
            int count =0;           
             for (int j = 0; j < c.length; j++) {                         
              if (i == c[j]-48){                   
                   count++;               
                    }            
             }            
             System.out.println(i+"出现的次数是:"+count+"次");       
           }   
      }
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值