千锋逆战班第22天

7.从命令行上读入一个字符串,用两种不同的方法,把该字符串转换为一个 int 类型
方法一:把 String 直接转换为 int
方法二:把 String 转换为 Integer,再把 Integer 转换为 int 类型
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String str = sc.nextLine();
//第一种做法,str --> int
int i1 = Integer.parseInt(str);
System.out.println(i1);
//第二种做法,str --> 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/位置,填入什么代码能编译通过?ABC
A. stu1 + “ ” + 100
B. 100 + “ ” + stu1
C. “ ” + 100 + stu1
D. stu1 + 100 + “ ”
11. 获取邮箱“[email protected]”中的用户名”zhengcg”。
public class Test1 {
public static void main(String[] args) {
String strings = “[email protected]”;
System.out.println(strings);
String[] s = strings.split("@");
System.out.println(s[0]);
}
}
12. 验证邮箱“[email protected]”是否是一个合法的邮箱格式。
public class Test2 {
public static void main(String[] args) {
String s = “[email protected]”;
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)中的”- ”去掉。
提示:java.util.UUID.randomUUID().toString() //可以获取随机 UUID
格式: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);
//replace()返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的
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();
char[] c = new char[4];
for (int i = 0; i < 4; i++) {
c[i] = s.charAt(random.nextInt(s.length()));
}
// System.out.println(Arrays.toString©);//打印数组
//将字符数组转化为字符串
String n = new String©;//第一种方法
System.out.println(n);
System.out.println(String.valueOf©);//第二种方法
}
}
15. 给定一个由数字组成的字符串 如:“1239586838923173478943890234092”;统计出每个数字出现的次数。
public class Test5 {
    public static void main(String[] args) {       
         String s = “1239586838923173478943890234092”;        c
         har[] c = s.toCharArray();      
          for (int i = 0;i <= 9;i++){          
           int count =0;          
            for (int j = 0; j < c.length; j++) {               
            //将字符串变换为字符数组后,将字符串转变为ASCII码与数字0-9进行比较              
             if (i == c[j]-48){                  
                  count++;              
                   }           
            }           
            System.out.println(i+“出现的次数是:”+count+“次”);      
          }  
     }
}

发布了23 篇原创文章 · 获赞 0 · 访问量 1949

猜你喜欢

转载自blog.csdn.net/funager/article/details/104639611