Java笔记

No_16_0307 Java基础学习第十二天

2016-03-07  本文已影响72人  lutianfei
文档版本 开发工具 测试平台 工程名字 日期 作者 备注
V1.0 2016.03.07 lutianfei none

[TOC]


Scanner类概述及其构造方法

基本格式

public class ScannerDemo {
    public static void main(String[] args) {
        // 创建对象
        Scanner sc = new Scanner(System.in);

        // 获取数据
        if (sc.hasNextInt()) {
            int x = sc.nextInt();
            System.out.println("x:" + x);
        } else {
            System.out.println("你输入的数据有误");
        }
    }
}

String 类

String类概述

构造方法

public class StringDemo {
    public static void main(String[] args) {
        // public String():空构造
        String s1 = new String();
        System.out.println("s1:" + s1);
        System.out.println("s1.length():" + s1.length());
        System.out.println("--------------------------");

        // public String(byte[] bytes):把字节数组转成字符串
        byte[] bys = { 97, 98, 99, 100, 101 };
        String s2 = new String(bys);
        System.out.println("s2:" + s2);//abcde
        System.out.println("s2.length():" + s2.length());//5
        System.out.println("--------------------------");

        // public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
        // 我想得到字符串"bcd"
        String s3 = new String(bys, 1, 3);
        System.out.println("s3:" + s3); //bcd
        System.out.println("s3.length():" + s3.length()); //3
        System.out.println("--------------------------");

        // public String(char[] value):把字符数组转成字符串
        char[] chs = { 'a', 'b', 'c', 'd', 'e', '爱', '林', '亲' };
        String s4 = new String(chs);
        System.out.println("s4:" + s4);
        System.out.println("s4.length():" + s4.length());
        System.out.println("--------------------------");

        // public String(char[] value,int index,int count):把字符数组的一部分转成字符串
        String s5 = new String(chs, 2, 4);
        System.out.println("s5:" + s5);//cde爱
        System.out.println("s5.length():" + s5.length());//4
        System.out.println("--------------------------");
        
        //public String(String original):把字符串常量值转成字符串
        String s6 = new String("abcde");
        System.out.println("s6:" + s6);
        System.out.println("s6.length():" + s6.length());
        System.out.println("--------------------------");
        
        //字符串字面值"abc"也可以看成是一个字符串对象。
        String s7 = "abcde";
        System.out.println("s7:"+s7);
        System.out.println("s7.length():"+s7.length());
    }
}
public class StringDemo3 {
    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        System.out.println(s1 == s2);// false
        System.out.println(s1.equals(s2));// true

        String s3 = new String("hello");
        String s4 = "hello";
        System.out.println(s3 == s4);// false
        System.out.println(s3.equals(s4));// true

        String s5 = "hello";
        String s6 = "hello";
        System.out.println(s5 == s6);// true
        System.out.println(s5.equals(s6));// true
    }
}
String s1 = "hello";
        String s2 = "world";
        String s3 = "helloworld";
        System.out.println(s3 == s1 + s2);// false 这里因为是变量相加先开空间再拼接
        System.out.println(s3.equals((s1 + s2)));// true

        System.out.println(s3 == "hello" + "world");// true 字符串是常量相加,先加然后在常量池里找如果有就直接返回,否则,就创建。
        System.out.println(s3.equals("hello" + "world"));// true

String类的判断功能

// boolean isEmpty():判断字符串是否为空。
System.out.println("isEmpty:" + s1.isEmpty());

String s4 = "";
String s5 = null;
System.out.println("isEmpty:" + s4.isEmpty());
// NullPointerException
// s5对象都不存在,所以不能调用方法,空指针异常
System.out.println("isEmpty:" + s5.isEmpty());
用户登录案例
/*
 * 模拟登录,给三次机会,并提示还有几次。
 * 
 * 分析:
 *      A:定义用户名和密码。已存在的。
 *      B:键盘录入用户名和密码。
 *      C:比较用户名和密码。
 *          如果都相同,则登录成功
 *          如果有一个不同,则登录失败
 *      D:给三次机会,用循环改进,最好用for循环。
 */
public class StringTest {
    public static void main(String[] args) {
        // 定义用户名和密码。已存在的。
        String username = "admin";
        String password = "admin";

        // 给三次机会,用循环改进,最好用for循环。
        for (int x = 0; x < 3; x++) {
            // x=0,1,2
            // 键盘录入用户名和密码。
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String name = sc.nextLine();
            System.out.println("请输入密码:");
            String pwd = sc.nextLine();

            // 比较用户名和密码。
            if (name.equals(username) && pwd.equals(password)) {
                // 如果都相同,则登录成功
                System.out.println("登录成功");
                break;
            } else {
                // 如果有一个不同,则登录失败
                // 2,1,0
                // 如果是第0次,应该换一种提示
                if ((2 - x) == 0) {
                    System.out.println("帐号被锁定,请与班长联系");
                } else {
                    System.out.println("登录失败,你还有" + (2 - x) + "次机会");
                }
            }
        }
    }
}

String类的获取功能

练习:遍历获取字符串中的每一个字符
public class StringTest {
    public static void main(String[] args) {
        // 定义字符串
        String s = "helloworld";
        for (int x = 0; x < s.length(); x++) {
            System.out.println(s.charAt(x));
        }
    }
}

练习:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
public class StringTest2 {
    public static void main(String[] args) {
        //定义一个字符串
        String s = "Hello123World";
        
        //定义三个统计变量
        int bigCount = 0;
        int smallCount = 0;
        int numberCount = 0;
        
        //遍历字符串,得到每一个字符。
        for(int x=0; x<s.length(); x++){
            char ch = s.charAt(x);
            
            //判断该字符到底是属于那种类型的
            if(ch>='a' && ch<='z'){
                smallCount++;
            }else if(ch>='A' && ch<='Z'){
                bigCount++;
            }else if(ch>='0' && ch<='9'){
                numberCount++;
            }
        }
        
        //输出结果。
        System.out.println("大写字母"+bigCount+"个");
        System.out.println("小写字母"+smallCount+"个");
        System.out.println("数字"+numberCount+"个");
    }
}

String类的转换功能

练习题:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
/
 * 分析:
 *      A:先获取第一个字符
 *      B:获取除了第一个字符以外的字符
 *      C:把A转成大写
 *      D:把B转成小写
 *      E:C拼接D
 */
public class StringTest {
    public static void main(String[] args) {
        // 定义一个字符串
        String s = "helloWORLD";

        // 先获取第一个字符
        String s1 = s.substring(0, 1);
        // 获取除了第一个字符以外的字符
        String s2 = s.substring(1);
        // 把A转成大写
        String s3 = s1.toUpperCase();
        // 把B转成小写
        String s4 = s2.toLowerCase();
        // C拼接D
        String s5 = s3.concat(s4);
        System.out.println(s5);

        // 优化后的代码
        // 链式编程
        String result = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());
        System.out.println(result);
    }
}

String类的其他功能

练习题1:
/*
 * 字符串反转
 * 举例:键盘录入”abc”     
 * 输出结果:”cba”
 * 
 * 分析:
 *      A:键盘录入一个字符串
 *      B:定义一个新字符串
 *      C:倒着遍历字符串,得到每一个字符
 *          a:length()和charAt()结合
 *          b:把字符串转成字符数组
 *      D:用新字符串把每一个字符拼接起来
 *      E:输出新串
 */
public class StringTest3 {
    public static void main(String[] args) {
        // 键盘录入一个字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String line = sc.nextLine();

        String s = myReverse(line);
        System.out.println("实现功能后的结果是:" + s);
    }

    /*
     * 两个明确: 
     返回值类型:String 
     参数列表:String
     */
    public static String myReverse(String s) {
        // 定义一个新字符串
        String result = "";

        // 把字符串转成字符数组
        char[] chs = s.toCharArray();

        // 倒着遍历字符串,得到每一个字符
        for (int x = chs.length - 1; x >= 0; x--) {
            // 用新字符串把每一个字符拼接起来
            result += chs[x];
        }
        return result;
    }
}
练习题2:
/*
 * 统计大串中小串出现的次数
 * 举例:
 *      在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"
 * 结果:
 *      java出现了5次
 * 
 * 分析:
 *      前提:是已经知道了大串和小串。
 * 
 *      A:定义一个统计变量,初始化值是0
 *      B:先在大串中查找一次小串第一次出现的位置
 *          a:索引是-1,说明不存在了,就返回统计变量
 *          b:索引不是-1,说明存在,统计变量++
 *      C:把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串
 *      D:回到B
 */
public class StringTest5 {
    public static void main(String[] args) {
        // 定义大串
        String maxString = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
        // 定义小串
        String minString = "java";

        // 写功能实现
        int count = getCount(maxString, minString);
        System.out.println("Java在大串中出现了:" + count + "次");
    }

    /*
     * 两个明确: 返回值类型:int 参数列表:两个字符串
     */
    public static int getCount(String maxString, String minString) {
        // 定义一个统计变量,初始化值是0
        int count = 0;
        int index;
        //先查,赋值,判断
        while((index=maxString.indexOf(minString))!=-1){
            count++;
            maxString = maxString.substring(index + minString.length());
        }

        return count;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读