API之常见对象

1.A:Scanner的概述

* B:Scanner的构造方法原理

    * Scanner(InputStream source)

    * System类下有一个静态的字段:

        * public static final InputStream in; 标准的输入流,对应着键盘录入。

 

* C:一般方法

    * hasNextXxx()  判断是否还有下一个输入项,其中Xxx可以是Int,Double等。如果需要判断是否包含下一个字符串,则可以省略Xxx

    * nextXxx()  获取下一个输入项。Xxx的含义和上个方法中的Xxx相同,默认情况下,Scanner使用空格,回车等作为分隔符

案例:

public class Demo_Scanner {

    public static void main(String[] args){

    

    Scanner sc = new Scanner(System.in);//键盘录入

    if(sc.hasNextInt()){//hasNextXxx判断下一个键盘录入类型,若为double,int直接写在Next后,若为字符型next后就不用写类型了

        int i= sc.nextInt();//将整型录入赋值给i

        System.out.println(i);

    }else{

        System.out.println("你输入的类型错误");

    }

}

}

2.nextInt();和nextLine()

案例

public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入第一个整数:");    

        int i = sc.nextInt();

        System.out.println("请输入第二个字符串:");    

        String line2 = sc.nextLine();    

        System.out.println("i = "+i+", line2 = "+line2);

        //为了证明line2中并没有存放\r\n,因为nextLine()遇到\r\n就结束了,所以不会存入line2    

        System.out.println(i);    

        System.out.println("111111111111");    

        System.out.print(line2);    

        System.out.println("22222222222");

输出:

请输入第一个整数:

1

请输入第二个字符串:

i = 1, line2 =

1

111111111111

22222222222

注意:

nextInt()是键盘录入整数的方法,当我们录入10的时候,其实在键盘上录入的是10\r\n,nextInt()方法只获取10就结束了
					

nextLine()是键盘录入字符串的方法,可以接收任意类型,但是他凭什么能获取一行呢?

通过\r\n,只要遇到\r\n就证明一行结束,nextLine()遇到\r\n就结束了
					

如何证明System.out.print(line2);执行了呢?

通过System.out.println("111111111111");    

        System.out.print(line2);    

        System.out.println("22222222222");来验证line2被执行了

另一种方法

public class Demo_Scanner {

 

    public static void main(String[] args) {

        System.out.println("请输入第一个数");

        Scanner sc = new Scanner(System.in);

        int i= sc.nextInt();

        System.out.println("请输入第二个数");

        Scanner sc1=new Scanner(System.in);

        String line=sc1.nextLine();

        System.out.println(i);

        System.out.println(line);

        

 

    }

 

}

输出

请输入第一个数

1

请输入第二个数

464

1

464

注意

这个就是创建了两个变量,Scanner类的对象可以调用nextInt()nextLine()的方法来调用,实现输入整数和各种类型,nextLine()可以输入任何类型变量。

2.常见类String类的概述

a:字符串字面值"abc"也可以看成是一个字符串对象。

b:字符串是常量,一旦被赋值,就不能被改变。

public class Demo_String {

    public static void main(String[] args){

        String str = "abc";//abc 可以当做一个字符串对象

        str="def";//当把"def"赋值给str,原来的abc成了垃圾

        System.out.println(str);//打印的结果不是"类名@字符串地址",而是"abc",说明String重写了toString类型

    }

 

}

3.String类的构造方法

public String():空构造

public String(byte[] bytes):把字节数组转成字符串

    public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串

    public String(char[] value):把字符数组转成字符串

    public String(char[] value,int index,int count):把字符数组的一部分转成字符串

    public String(String original):把字符串常量值转成字符串

 

public class Demo_String2 {

    public static void main(String[] args){

        String s1 = new String();//空参构造

        System.out.println(s1);

          

        

        byte[] arr1={97,98,99};

        String s2 = new String(arr1);//解码将计算机能读懂的转换为我们能读懂的,将数字数组转换成字母

        System.out.println(s2);

        

        byte[] arr2={97,98,99,100,101,102};

        String s3= new String(arr2,2,3);//将部分数组转换成字母,从第三个数字开始,3

        System.out.println(s3);

        

        byte[] arr3={'a','b','c','d','e'};//将字符数组转换成字符串

        String s4= new String(arr3);

        System.out.println(s4);

        

        String s5= new String(arr3,1,3);//将部分字符数组转换成字符串

        System.out.println(s5);

        

        String s6= new String("heima");//输出字符串

        System.out.println(s6);

        

    }

 

}

4.String的常量值与堆内存的比较

A==比较字符串地址是否相同

Bequals(变量)方法比较内容是否相同

C.字符串

Dnew 对象在堆中,和对象有关的如this,new对象,数组都在堆中

package com.itheima.string;

public class Demo3_String {    

    public static void main(String[] args) {

        //demo1();

        //demo2();

        //demo3();

        //demo4();

        demo5();

    }

    private static void demo5() {

        String s1 = "ab";

        String s2 = "abc";

        String s3 = s1 + "c";

        System.out.println(s3 == s2); //false 因为s1是变量,在堆中通过StringBuffer同过+串起来然后通过toString方法将之由字符转变为字符串

        System.out.println(s3.equals(s2));         //true

    }

    private static void demo4() {

        //byte b = 3 + 4;                //在编译时就变成7,7赋值给b,常量优化机制

        String s1 = "a" + "b" + "c";//java中有常量优化机制,在编译时期就能确定s2的值为"abc",所以编译时期,在常量池中创建"abc"

        String s2 = "abc";//执行到这里时常量池中已经有了"abc",所以就不再创建,所以s1s2指向的是常量池中同一个字符串常量"abc"

        System.out.println(s1 == s2);             //true,java中有常量优化机制    

        System.out.println(s1.equals(s2));     //true

    }

    private static void demo3() {//==比较的是地址值

        String s1 = new String("abc");            //录的是堆内存对象的地址值        

        String s2 = "abc";                        //记录的是常量池中的地址值

        System.out.println(s1 == s2);             //false

        System.out.println(s1.equals(s2));         //true

    }

    private static void demo2() {

        //创建几个对象

        //创建两个对象,一个在常量池中,一个在堆内存中

        String s1 = new String("abc");        

        System.out.println(s1);

    }

    private static void demo1() {                //常量池中没有这个字符串对象,就创建一个,如果有直接用即可

        String s1 = "abc";

        String s2 = "abc";

        System.out.println(s1 == s2);             //==号比较的是地址值,true

        System.out.println(s1.equals(s2));     //比较的是字符串的内容:true

    }

}

 

 

1(针对demo1方法):

针对demo3方法

 

针对demo5方法
					


				

案例:

public class Demo_String2 {

    public static void main(String[] args){

        String a="A";

        String b="A";

        System.out.println(a==b);

        Person q= new Person("A",23);

        Person w= new Person("A",21);

        System.out.println(q.name==w.name);//虽然创建两个对象但是对象调用的name属性都是从常量池中一个地方调用

        

    }

}

    

class Person{

    String name;

    int age;

    public Person(String name, int age) {

        super();

        this.name = name;

        this.age = age;

    }

    public Person() {

        super();

        // TODO Auto-generated constructor stub

    }

    

}

输出:

true

true

5.String类的判断功能

String类的判断功能

比较字符串的内容是否相同,区分大小写

比较字符串的内容是否相同,忽略大小写

判断大字符串中是否包含小字符串

判断字符串是否以某个指定的字符串开头

判断字符串是否以某个指定的字符串结尾

判断字符串是否为空。

 

package com.heima.string;

public class Demo4_StringMethod {

 

    public static void main(String[] args) {

        //demo1();

        //demo2();

        String s1 = "heima";

        String s2 = "";

        String s3 = null;

        

        System.out.println(s1.isEmpty());

        System.out.println(s2.isEmpty());

        System.out.println(s3.isEmpty());    //java.lang.NullPointerException    " "空字符常量同时也是String

对象,当然可以调用String类方法

//null时空常量,不能用任何的方法,否则就会出现空指针异常java.lang.NullPointerExceptionnull常量可以给任何引用数据类型赋值,引用数据类型的变量都是对象

 

}

    private static void demo2() {

        String s1 = "我爱heima,哈哈";

        String s2 = "heima";

        String s3 = "baima";

        String s4 = "我爱";

        String s5 = "哈哈";

        

        System.out.println(s1.contains(s2));        //判断是否包含传入的字符串

        System.out.println(s1.contains(s3));

        

        System.out.println("------------------");

        System.out.println(s1.startsWith(s4));        //判断是否以传入的字符串开头

        System.out.println(s1.startsWith(s5));

        

        System.out.println("------------------");

        System.out.println(s1.endsWith(s4));        //判断是否以传入的字符串结尾

        System.out.println(s1.endsWith(s5));

    }

    private static void demo1() {

        String s1 = "heima";

        String s2 = "heima";

        String s3 = "HeiMa";

        

        System.out.println(s1.equals(s2));        //true

        System.out.println(s2.equals(s3));        //false

        

        System.out.println("---------------");

        

        System.out.println(s1.equalsIgnoreCase(s2));    

        System.out.println(s1.equalsIgnoreCase(s3));     //不区分大小写

    }

}

6.模拟用户登录

需求:模拟登录,给三次机会,并提示还有几次。

* 用户名和密码都是admin

分析:

    * 1,模拟登录,需要键盘录入,Scanner

    * 2,给三次机会,需要循环,for

    * 3,并提示有几次,需要判断,if

public class Test1 {

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        for(int i=0;i<3;i++){

            System.out.println("请输入用户名");

            String a=sc.nextLine();

            System.out.println("请输入密码");

            String q=sc.nextLine();

            if("admin".equals(a)&&"admin".equals(q)){

                System.out.println("恭喜你登陆成功");    

break;                            

            }else{if(i==2){

                System.out.println("请你明天再来");

                break;

            }System.out.println("你还有"+(2-i)+"次机会");

                    

                

            }

            

        }

    }

 

}

第二种方法

public class Test1 {

    public static void main(String[] args){

    Scanner sc = new Scanner(System.in);

    int count=3;

    while(true){

        System.out.println("请输入你的姓名");

        String a=sc.nextLine();

        System.out.println("请输入密码");

        String b=sc.nextLine();

        if("admin".equals(a)&&"admin".equals(b)){

            System.out.println("欢迎"+a+"回来");

            break;

        }else{

            if(--count==0){

                System.out.println("请明天再来");

                break;

            }else{

                System.out.println("你还剩"+count+"机会");

                

            }

        }

    }

    }

 

}

注意:while循环语句()里加判断条件或者turefalse,里面语句只要不满足条件就逐条运行,如本案例中—count一直在递减。如果需要键盘录入就创建Scanner类的对象调用特定方法如nextInt(),hasNext数据类型(判断下一个输入类型是否满足类型)

posted @ 2016-11-18 00:19  小彭快跑  阅读(335)  评论(0编辑  收藏  举报