String案例之模拟用户3次登录:
1 public static void main(String[] args) { 2 //定义正确的登录名和密码 3 String rightName = "admin"; 4 String rightPassword = "123456"; 5 //定义一个扫描器对象接收用户输入的数据 6 Scanner sc = new Scanner(System.in); 7 8 for (int i = 0; i < 3; i++) { 9 System.out.println("输入登录名:"); 10 String inputName = sc.next(); 11 System.out.println("输入密码:"); 12 String inputPassword = sc.next(); 13 if (rightName.equals(inputName) && rightPassword.equals(inputPassword)){ 14 System.out.println("登录成功!"); 15 break; 16 } 17 else { 18 System.out.println("登录失败~"); 19 System.out.println("你还有" + (2-i) + "次机会!"); 20 } 21 } 22 }
String案例之随机生成5位数验证码:
1 public static void main(String[] args){ 2 //1、定义可能出现的全部信息 3 String allChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 4 //2、定义一个5次的循环,每循环一次取出一个字符 5 String randCode = ""; 6 Random rand = new Random(); 7 for (int i = 0; i < 5; i++) { 8 int randNumber = rand.nextInt(allChars.length()-1); //范围 [0 ~ x) 9 randCode += allChars.charAt(randNumber); 10 } 11 System.out.println("你好,你的验证码为:" + randCode); 12 }
String案例之手机号码隐藏(以字符串的形式从键盘中接收一个手机号,将中间四位号码隐藏为*,最终效果为:155****5986):
1 public static void main(String[] args){ 2 //1、定义一个扫描器对象用于接收用户输入的号码 3 Scanner sc = new Scanner(System.in); 4 System.out.println("输入手机号:"); 5 String phoneNumber = sc.next(); 6 //2、截取字符串前三位、后四位、最后和****相加 7 System.out.println(phoneNumber); 8 String formerThree = phoneNumber.substring(0,3); 9 String laterFour = phoneNumber.substring(7); 10 String shieldNumber = formerThree + "****" + laterFour; 11 System.out.println(shieldNumber); 12 }
ArrayList模拟电影院排片系统:
1、定义一个Movie类:
1 public class Movie { 2 private String filmTitle; //影片名 3 private double grade; //评分 4 private String actor; //主演 5 6 public Movie() { 7 } 8 9 public Movie(String filmTitle, double grade, String actor) { 10 this.filmTitle = filmTitle; 11 this.grade = grade; 12 this.actor = actor; 13 } 14 15 public String getFilmTitle() { 16 return filmTitle; 17 } 18 19 public void setFilmTitle(String filmTitle) { 20 this.filmTitle = filmTitle; 21 } 22 23 public double getGrade() { 24 return grade; 25 } 26 27 public void setGrade(double grade) { 28 this.grade = grade; 29 } 30 31 public String getActor() { 32 return actor; 33 } 34 35 public void setActor(String actor) { 36 this.actor = actor; 37 } 38 }
2、定义一个ArrayList集合存放Movie类的实例对象:
1 public static void main(String[] args) { 2 ArrayList<Movie> movies = new ArrayList<>(); 3 movies.add(new Movie("肖申克的救赎",9.8,"安迪")); 4 movies.add(new Movie("迪迦奥特曼",9.9,"大古")); 5 movies.add(new Movie("熊出没之夺宝雄兵",9.5,"光头强、熊大、熊二")); 6 for (int i = 0; i < movies.size(); i++) { 7 System.out.println("影片名:" + movies.get(i).getFilmTitle() + " " + 8 "评分:" + movies.get(i).getGrade() + " " + 9 "主演:" + movies.get(i).getActor()); 10 } 11 }
ArrayList集合模拟学生信息搜索(在集合中根据一个学生的学号搜索是否存在该学生,存在则输出该生具体信息,不存在则给出相应提示):
1、定义一个学生类
1 public class Student { 2 private String id; //学号 3 private String name; //学生姓名 4 private int age; //年龄 5 private String className; //班级 6 7 public Student() { 8 } 9 10 public Student(String id, String name, int age, String className) { 11 this.id = id; 12 this.name = name; 13 this.age = age; 14 this.className = className; 15 } 16 17 public String getId() { 18 return id; 19 } 20 21 public void setId(String id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public int getAge() { 34 return age; 35 } 36 37 public void setAge(int age) { 38 this.age = age; 39 } 40 41 public String getClassName() { 42 return className; 43 } 44 45 public void setClassName(String className) { 46 this.className = className; 47 } 48 }
2、定义一个ArrayList集合存储学生信息
1 public static void main(String[] args){ 2 ArrayList<Student> students = new ArrayList<>(); 3 students.add(new Student("20180302","叶孤城",23,"护理1班")); 4 students.add(new Student("20180303","东方不败",23,"推拿2班")); 5 students.add(new Student("20180304","西门吹雪",26,"中医药学4班")); 6 students.add(new Student("20180305","梅超风",26,"神经科2班")); 7 8 for (int i = 0; i < students.size(); i++) { 9 System.out.println("学号 " + "\t\t\t" + "姓名" + "\t\t\t" + " 年龄" + "\t\t\t" + " 班级"); 10 System.out.println(students.get(i).getId() + "\t\t\t" + students.get(i).getName() + "\t\t\t" + 11 students.get(i).getAge() + "\t\t\t" + students.get(i).getClassName()); 12 } 13 14 //3、提供一个方法,传入ArrayList集合和学号可以得到学生对象 15 Scanner sc = new Scanner(System.in); 16 while (true) { 17 String inputId = sc.next(); 18 Student ret = serachStuIofoById(students,inputId); 19 if(ret == null){ 20 System.out.println("不存在该学生"); 21 } 22 else { 23 System.out.println("找到了"); 24 System.out.println("学号:" + ret.getId() + 25 "姓名:" + ret.getName() + 26 "年龄:" + ret.getAge() + 27 "班级:" + ret.getClassName()); 28 break; 29 } 30 } 31 } 32 33 //根据学号在集合中查找学生 34 private static Student serachStuIofoById(ArrayList<Student> students, String id) { 35 for (int i = 0; i < students.size(); i++) { 36 if(id.equals(students.get(i).getId())){ 37 return students.get(i); 38 } 39 } 40 return null; 41 }