黑马——api初识

API初识

package com.itheima.api;

import java.util.Scanner;

public class demo1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入");
        //调用nextline方法
        //快捷键是ctrl+alt +v快速生成返回值
        String s = sc.nextLine();
        System.out.println(s);
        //next()方法遇到空格就结束nextline()是回车结束


    }
}

在nextline()方法和nextint()方法同时使用的时候由于回车的原因会导致直接结束,解决方法就是使用next()接受字符输入

package com.itheima.api;

import java.util.Scanner;

public class demo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入");
        int num = sc.nextInt();
        System.out.println("请输入字符串");
        String s = sc.next();
        System.out.println(num);
        System.out.println(s);
    }
}

string

字符串是常量一旦创建之后就无法更改了,使用的是""创建的字符串只要是相同的只会创建一次对象,当再次出现的时候回去字符串常量池调用(字符串常量池在堆内存里面),使用构造方法创建字符串的时候因为对象不一样是因此两个对象是不相等的。

要想比较字符串的话可以使用的是equals方法进行比较

package com.itheima.api.Stringmethod;

public class demo1 {
    public static void main(String[] args) {
        String  s1 = "abc";
        String  s2 = "ABC";
        String  s3 = "abc";

        boolean equals = s1.equals(s2);
        System.out.println(equals);
        boolean equals1 = s1.equals(s2);
        System.out.println(equals1);

    }
}

实现用户的登录

package com.itheima.api.tst;
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        String username = "niuma";
        String password = "1234556";
        for (int i = 0; i < 3; i++) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名");
            String my_username = scanner.nextLine();
            System.out.println("请输入密码");
            String my_password = scanner.nextLine();
            if(my_password.equals(username) && my_password.equals(password))
            {
                System.out.println("登录成功");
                break;

            }
            else{
                System.out.println("错误重新输入");
            }

        }
    }
}

遍历字符串的每一个字母

package com.itheima.api.tst;

import java.util.Scanner;

public class test2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字符串");
        String s = sc.nextLine();

        for(int i = 0;i<s.length();i++)
        {
            System.out.println(s.charAt(i));

        }
    }
}

输出字符串里面有多少个数字小写字母和大写字母:

package com.itheima.api.tst;

import java.util.Scanner;

public class test03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        int bigcount = 0;
        int smallcount = 0;
        int numcount = 0;
        char[] chars = s.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (c >= 'a' && c <= 'z') {
                smallcount++;
            } else if (c >= 'A' && c <= 'Z') {
                bigcount++;

            }
            else
            {
                numcount++;


            }
        }

        System.out.println(bigcount);
        System.out.println(smallcount);
        System.out.println(numcount);
    }
}

替换敏感词:例如将tmd替换为***号

package com.itheima.api.tst;

import java.util.Scanner;

public class test5 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String ss= s.replace("tmd","***");
        System.out.println(ss);
    }
}


切割字符串:根据逗号切割字符串提取有效信息

package com.itheima.api.tst;

import com.itheima.api.domain.Student;

import java.util.Scanner;

public class test7 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生信息");
        String stuinfo = scanner.nextLine();
        String[] sarr = stuinfo.split(",");

        Student stu = new Student(sarr[0],sarr[1]);
        System.out.println(stu.getName());
        System.out.println(stu.getAge());
    }
}
//使用的student类自己实现就好了捏
posted @   怪树  阅读(56)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示