千锋逆战班
在千锋"逆战”学习第21天,
每个人生阶段都需要努力去扮好自己的角色,越努力越轻松,越坚强越幸运!
加油!
1
从命令行上读入一个字符串,用两种不同的方法,把该字符串转换为-个int类型
方法- :把String直接转换为int
方法二:把String转换为Integer ,再把Integer转换为int类型
public class Test7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int i1=Integer.parseInt(str);
Integer i2=new Integer(str);
int i3=i2.intValue();
}
}
2( 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) {
// TODO Auto-generated method stub
Student stu2=new Student("Tom",18);
}
}问:在/*1*/位置,填入什么代码能编译通过?
A.stul+“”+ 100
B.100+"”+ stul
C.“”+ 100+ stu1
D.stul + 100+ " ”
答案为ABC
3.获取邮箱"zhengcg@zparkhr.com" 中的用户名" zhengcg" .
public class Text11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String i="zhengcg@zparhr.com";
String[] str;
str=i.split("@");
System.out.println(str[0]);
}
}
4验证邮箱"zhengcg@zparkhr.com" 是否是一个合法的邮箱格式。提示:
I. 邮箱必须包含“@”和" . "
II. 最后一个".“的位置必须大于”@"的位置。
public class Test12 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="zhengcg@zparhr.com";
int i1=s.indexOf("@");
int i2=s.indexOf(".");
if(i1<i2) {
System.out.println("此邮箱合法");
}
}
}
5将随机获取的UUID (含义是通用唯一识别码Universally Unique Identifier)中的" -"去掉。
提示: java.util.UID.randomUUID0).toString0 //可以获取随机UUID
格式: e6c57443-1667 -4d75-98f6- a8863d95e58f
public class Test13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s=java.util.UUID.randomUUID().toString();
System.out.println(s);
System.out.println(s.replace("-", ""));
}
}
6在给定的字符串"ABCDEFGhjklmn1234567" 中获取随机的4个字符,并使用StringBuilder拼接成字符串。( 随机获取到的4个字符中可以出现重复字符)
提示:创建随机数对象java.util.Random.
java.util.Random random = new java.util.Random0;
random.nextInt(100); //可以获取到0~99中任意-个随机数
public class Test14 {
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);
}
}
7给定一个由数字组成的字符串如:"1239586838923173478943890234092* ;统计出每个数字出现的次数。
public class Test18 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="1239586838923173478943890234092";
char[] c=s.toCharArray();
System.out.println((int)c[0]);
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+"次");
}
}
}
49
数字0出现2次
数字1出现2次
数字2出现4次
数字3出现6次
数字4出现3次
数字5出现1次
数字6出现1次
数字7出现2次
数字8出现5次
数字9出现5次