2019-06-04 java学习日记

例子:模拟登录

需求:模拟登录,给三次机会,并提示还有几次。
用户名和密码都是admin
分析:
1,模拟登录,需要键盘录入,Scanner
2,给三次机会,需要循环,for
3,并提示有几次,需要判断,if

 1 import java.util.Scanner;   //导入键盘录入对象的包
 2 
 3 public class test1 {
 4         public static void main(String[] args) {
 5         for (int i = 0; i < 3; i++) {
 6         //创建键盘录入对象
 7         Scanner sc = new Scanner(System.in);
 8         //将键盘录入的用户名存储在userName中
 9         String userName = sc.nextLine();
10         //将键盘录入的密码存储在password中
11         String password = sc.nextLine();
12      //如果是字符串常量和字符串变量比较,通常都是字符串常量调用方法,将变量当作参数传递,防止空指针异常
13         if ("admin".equals(userName) && "admin".equals(password)) {
14             System.out.println("欢迎" + userName + "登录");
15             //跳出循环
16             break;
17         } else {
18             if (i == 2) {
19                 System.out.println("您的错误次数已到,请明天再来吧");
20             } else {
21                 System.out.println("录入错误,您还有" + (2 - i) + "次机会");
22             }
23         }
24         
25       }
26         
27     }
28 
29 }

字符串的遍历

 1 public class test3 {
 2 
 3     public static void main(String[] args) {
 4         String s = "woaining";
 5         for (int i = 0; i < s.length(); i++) {
 6             System.out.print(s.charAt(i));
 7         }
 8         /*或者
 9          * System.out.println("-------------");
10         for (int j = 0; j < s.length(); j++) {
11             char c = s.charAt(j);
12             System.out.print(c);
13         }*/
14     }
15 
16 }

统计不同类型字符个数

 1 package com.practice1.text1;
 2 
 3 public class tset4 {
 4 
 5     public static void main(String[] args) {
 6         String s = "AG2@*Lg8a#!^$5H5a54fOGgH";
 7         int A = 0;
 8         int a = 0;
 9         int numb = 0;
10         int other = 0;
11         //获取每一个字符,通过for循环遍历
12         for (int i = 0; i < s.length(); i++) {
13         //通过索引获取每一个字符
14             char c = s.charAt(i);
15         //判断字符是否在这个范围内
16             if (c > 'A' && c < 'Z') {
17         //如果满足是大写字母,就让其对应的变量自增
18                 A++;
19             } else if (c > 'a' && c < 'z') {
20                 a++;
21             } else  if (c > '0' && c < '9') {
22                 numb++;
23             } else {
24                 other++;
25             }
26         }
27         //打印每一个计数器的结果
28         System.out.println(s + "中" + "大写字母个数为:" + A + "小写字母个数为:" 
29         + a + "数字个数为:" + numb + "其他类型字符个数为:" + other);
30     }
31         
32 }

String类的其他功能

注:

1,char的转换都是用的unicode码表

2,从字符串转换为字节,字节转换字符串的,用的是GBK码表

3,这两种码表都包含了ASCLL码表

String的替换功能

 String replace(char old,char new)
 String replace(String old,String new)

 1 private static void demo1() {
 2         String s = "heima";
 3         String s2 = s.replace('i', 'o');            //用o替换i
 4         System.out.println(s2);
 5         
 6         String s3 = s.replace('z', 'o');            //z不存在,保留原字符不改变
 7         System.out.println(s3);
 8         
 9         String s4 = s.replace("ei", "ao");
10         System.out.println(s4);
11     }

 

String的去除字符串两空格 

String trim()

1 private static void demo2() {
2         String s = "   hei   ma   ";
3         String s2 = s.trim();
4         System.out.println(s2);
5     }

String的按字典顺序比较两个字符串

int compareTo(String str)
int compareToIgnoreCase(String str)

 1 public static void main(String[] args) {
 2         String s1 = "a";
 3         String s2 = "aaaa";
 4         
 5         int num = s1.compareTo(s2);        //按照码表值比较
 6         System.out.println(num);
 7         
 8         String s3 = "黑";
 9         String s4 = "马";
10         int num2 = s3.compareTo(s4);
11         System.out.println('黑' + 0);        //查找的是unicode码表值
12         System.out.println('马' + 0);
13         System.out.println(num2);
14         
15         String s5 = "heima";
16         String s6 = "HEIMA";
17         int num3 = s5.compareTo(s6);
18         System.out.println(num3);
19         
20         int num4 = s5.compareToIgnoreCase(s6);
21         System.out.println(num4);

将一个字符串首字母转换为大写,其余为小写(用链式编程)

public class test2 {

    public static void main(String[] args) {
        String s1 = "ynwIaPgf";
        String s2 = s1.substring(0, 1).toUpperCase()
                .concat(s1.substring(1).toLowerCase());
        /*用substring方法把第一个字母截取出来后用toUppercase方法把他转换为大写
         * 用substring方法把从第二个字母开始截取出来后用toLowercase方法把他转换为小写
         * 再用concat方法把它们之间连起来*/
        System.out.println(s2);
    }

}

字符串的反转

需求:把字符串反转
举例:键盘录入"abc"
输出结果:"cba"
分析:
1,通过键盘录入获取字符串Scanner
2,将字符串转换成字符数组
3,倒着遍历字符数组,并再次拼接成字符串
4,打印

public static void main(String[] args) {
        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        //将键盘录入的字符串存储在s1中
        String s1 = sc.nextLine();
        //将字符串转换为字符数组
        char[] c = s1.toCharArray();
        //创建一个s2储拼接出来的字符串
        String s2 = "";
        //倒着遍历字符数组
        for (int i = c.length-1; i >= 0 ; i--) {
            //拼接成字符串
            s2 += c[i];
        }
        System.out.println(s2);
    }
原文地址:https://www.cnblogs.com/Sherwin-liao/p/10976441.html