Common objects of JAVA advanced (1)

1: The use of Scanner

(1)在JDK5以后出现的用于键盘录入数据的类。
(2)构造方法:
	A:System.in
		它是标准的输入流,对应于键盘录入
	B:构造方法
		InputStream is = System.in;
		
		Scanner(InputStream is)
	C:常用的格式
		Scanner sc = new Scanner(System.in);
(3)基本方法格式:
	A:hasNextXxx() 判断是否是某种类型的
	B:nextXxx()	返回某种类型的元素
(4)要掌握的两个方法
	A:public int nextInt()
	B:public String nextLine()
(5)需要注意的小问题
	A:同一个Scanner对象,先获取数值,再获取字符串会出现一个小问题。
	B:解决方案:
		a:重新定义一个Scanner对象
		b:把所有的数据都用字符串获取,然后再进行相应的转换

2: Overview and use of the String class

(1)多个字符组成的一串数据。
	其实它可以和字符数组进行相互转换。
(2)构造方法:
	A:public String()
	空构造
	B:public String(byte[] bytes)
	将字节数组转变成字符串
	C:public String(byte[] bytes,int offset,int length)
	将字节数组的一部分转变成字符串
	D:public String(char[] value)
	将字符数组转变成字符串
	E:public String(char[] value,int offset,int count)
	将字符数组的一部分转变成字符串
	F:public String(String original)
	将字符串常量值转变成字符串
	下面的这一个虽然不是构造方法,但是结果也是一个字符串对象
	G:String s = "hello";
(3)字符串的特点
	A:字符串一旦被赋值,就不能改变。
		注意:这里指的是字符串的内容不能改变,而不是引用不能改变。
	B:字面值作为字符串对象和通过构造方法创建对象的不同
		String s = new String("hello");和String s = "hello"的区别?
		前者创建2个对象,一个在堆中,一个在方法区的字符串常量池中
		后者创建一个对象,在方法区中
(4)字符串的相关题(看程序写结果)
	A:==和equals()
		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
	B:字符串的拼接
		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
		注意“hello”和“world”是常量字符串
		System.out.println(s3.equals("hello" + "world"));// true
(5)字符串的功能(自己补齐方法中文意思)
	A:判断功能
		boolean equals(Object obj)
		判断是否相等,区分大小写
		boolean equalsIgnoreCase(String str)
		判断是否相等忽略大小写
		boolean contains(String str)
		判断是否含有字符串
		boolean startsWith(String str)
		判断是否以字符处开头
		boolean endsWith(String str)
		判断是否以字符串结尾
		boolean isEmpty()
		判断字符串是否为空,是指对象为空,而不是值为空
		对象为空:String s=null;
		值为空:String s="";
	B:获取功能
		int length()
		获取长度
		char charAt(int index)
		获取索引值对应的字符
		int indexOf(int ch)
		返回指定字符在此字符串中第一次出现的索引
		int indexOf(String str)
		返回指定字符串在此字符串中第一次出现的索引
		int indexOf(int ch,int fromIndex)
		返回指定字符在此字符串中从指定位置后第一次出现处的索引
		int indexOf(String str,int fromIndex)
		返回指定字符串在此字符串中从指定位置后第一次出现处的索引
		String substring(int start)
		从指定位置开始截取字符串,默认到末尾
		String substring(int start,int end)
		从指定位置开始到指定位置结束截取字符串		
	C:转换功能
		byte[] getBytes()
		将字符串转化成字节数组
		char[] toCharArray()
		将字符串转化成字符数组
		static String valueOf(char[] chs)
		将字符数组转化成字符串
		static String valueOf(int i)
		讲int型的数据转化成字符串
		String toLowerCase()
		将字符串转化成大写
		String toUpperCase()
		将字符串转化成小写
		String concat(String str)
		将字符串进行拼接
	D:其他功能
		a:替换功能 
			String replace(char old,char new)
			String replace(String old,String new)
		b:去空格功能
			String trim()
		c:按字典比较功能
			int compareTo(String str)
			int compareToIgnoreCase(String str) 
(6)字符串的案例
	A:模拟用户登录
	B:字符串遍历
	C:统计字符串中大写,小写及数字字符的个数
	D:把字符串的首字母转成大写,其他小写
	E:把int数组拼接成一个指定格式的字符串
	F:字符串反转
	G:统计大串中小串出现的次数

Case

Simulate login, give three chances, and prompt a few more times. If the login is successful, you can play the number guessing game.

import java.util.Scanner;
public class Test {
    
    
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("登录成功,开始玩游戏");
				//猜数字游戏
				GuessNumberGame.start();
				break;
			} else {
    
    
				// 如果有一个不同,则登录失败
				// 2,1,0
				// 如果是第0次,应该换一种提示
				if ((2 - x) == 0) {
    
    
					System.out.println("帐号被锁定,请与客服联系");
					break;
				} else {
    
    
					System.out.println("登录失败,你还有" + (2 - x) + "次机会");
				}
			}
		}
	}
}

import java.util.Scanner;
public class GuessNumberGame {
    
    
	private GuessNumberGame() {
    
    
	}

	public static void start() {
    
    
		// 产生一个随机数
		int number = (int) (Math.random() * 100) + 1;

		while (true) {
    
    
			// 键盘录入数据
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入你要猜的数据(1-100):");
			int guessNumber = sc.nextInt();

			// 判断
			if (guessNumber > number) {
    
    
				System.out.println("你猜的数据" + guessNumber + "大了");
			} else if (guessNumber < number) {
    
    
				System.out.println("你猜的数据" + guessNumber + "小了");
			} else {
    
    
				System.out.println("恭喜你,猜中了");
				break;
			}
		}
	}
}

Insert picture description here
Traverse to get every character in the string

import java.util.Scanner;
public class Test {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("请输入一个字符串");
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        for (int i=0;i<str.length();i++){
    
    
            System.out.println(str.charAt(i));
        }
    }
}

Insert picture description here
Count the number of occurrences of uppercase alphabetic characters, lowercase alphabetic characters, and numeric characters in a string. (Do not consider other characters)

import java.util.Scanner;
public class Test {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("请输入一个字符串");
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        int bigcount=0;
        int smallcount=0;
        int numcount=0;
        for (int i=0;i<str.length();i++){
    
    
            char c=str.charAt(i);
            if('a'<=c&&c<='z'){
    
    
                smallcount++;
            }
            else if('A'<=c&&c<='Z'){
    
    
                bigcount++;
            }
            else if('0'<c&&c<'9'){
    
    
                numcount++;
            }
        }
        System.out.println("数字"+numcount);
        System.out.println("大写字母"+bigcount);
        System.out.println("小写字母"+smallcount);
    }
}

Insert picture description here

Convert the first letter of a string to uppercase and the rest to lowercase. (Only English uppercase and lowercase alphabetic characters are considered)

import java.util.Scanner;
public class Test {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("请输入一个字符串");
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        String s1=str.substring(0,1);
        String s2=str.substring(1);
        System.out.println(s1.toUpperCase().concat(s2.toLowerCase()));
    }
}

Insert picture description here
Concatenate the data in the array into a string according to the specified format

public class StringTest {
    
    
	public static void main(String[] args) {
    
    
		// 前提是数组已经存在
		int[] arr = {
    
     1, 2, 3 };

		// 定义一个字符串对象,只不过内容为空
		String s = "";

		// 先把字符串拼接一个"["
		s += "[";

		// 遍历int数组,得到每一个元素
		for (int x = 0; x < arr.length; x++) {
    
    
			// 先判断该元素是否为最后一个
			if (x == arr.length - 1) {
    
    
				// 就直接拼接元素和"]"
				s += arr[x];
				s += "]";
			} else {
    
    
				// 就拼接元素和逗号以及空格
				s += arr[x];
				s += ", ";
			}
		}

		// 输出拼接后的字符串
		System.out.println("最终的字符串是:" + s);
	}
}

Insert picture description here
String reverse

import java.util.Scanner;
public class Test {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("请输入一个字符串");
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        System.out.println(Myreverse(str));
    }
    public static String Myreverse(String s){
    
    
        String result="";
        char[] c=s.toCharArray();
        for(int i=c.length-1;i>=0;i--){
    
    
            result=result+c[i];
        }
        return result;
    }

}

Insert picture description here
Count the number of occurrences of small and large strings

import java.util.Scanner;
public class Test {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("请输入一串长字符串");
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        System.out.println("请输入要统计的小字符串");
        String s=sc.nextLine();
        int count=Mincount(str,s);
        System.out.println("该字符串中"+s+"的个数为"+count);
    }
    public static int Mincount(String bs,String ss){
    
    
        int count=0;
        int index=bs.indexOf(ss);
        while (index!=-1){
    
    
            count++;
            index=index+ss.length();
            index=bs.indexOf(ss,index);
        }

        return count;
    }

}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45798550/article/details/107912575