问题:
 在使用scanner的时候如果先读取一个数字,在读取一行带有空格的字符串,势必会出错或者字符串读不到
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        int x = scanner.nextInt();
        String s = scanner.nextLine();
        
        System.out.println("x = " + x);
        System.out.println("s = " + s);    
}
错误原因:标记读取方法 nextInt() 读取数字,然后在分隔符处停止,这里的分隔符是行分隔符(回车键),nextLine() 会在读取行分隔符之后结束,然后返回行分隔符之前的字符串,因为行分隔符之前没有字符,所以line是空的。
解决方法:因为我们前面读取的是一个数字,我们可以先用字符串读取这个数字,在用字符串读取我们所要读取的字符,然后再把第一个读取的字符串转换为数字就可以实现这个功能了。
 两种解决方法:
解决方法一:将数字先转化为字符串型,再转化回int型
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        String y = scanner.nextLine();
        String s = scanner.nextLine();
        
        int x = Integer.parseInt(y);               //强制转换字符y为int类型的x
        
        System.out.println("x = " + x);
        System.out.println("s = " + s);    
}
解决方法二:用next()替代nextLine(),但是这里的next无法读取有空格的字符串,但可以读取字符串
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        int x = scanner.nextInt();
        String s = scanner.next();
        
        System.out.println("x = " + x);
        System.out.println("s = " + s);    
}
参考:java学习笔记:Scanner先输入整型再输入字符串时,字符串不能输入的问题
科大讯飞笔试编程第二题:
 找两个字符串相似度
- 两个字符串长度相同,相似加1
- 两个字符串含有相同类型的字符组成,相似加1
 相似类型的字符只有三类 数字、字母、其他字符
 第一行输入t,它代表查询几次
 每次查询输入两个字符串比较相似
 比如:

 通过分析可知0>=相似度<=2
public class Main {
	public static void main(String args[]) {		
		Scanner sc=new Scanner(System.in);
		String y=sc.nextLine();
		int t=Integer.parseInt(y);
		//不能先输入数字型再输入字符串型(特指nextLine)
		String[] str=new String[2];
		
		while(t>0) {
			
			int[] re1=new int[3];
			int[] re2=new int[3];
			
			int count=0;
			t--;			
		
			
			str[0]=sc.nextLine();//输入带空格的字符串
		
			str[1]=sc.nextLine();
			
			
			if(str[0].length()==str[1].length()) {//第一个判定条件
				count++;
			}
			char[] ch1=new char[str[0].length()];
			char[] ch2=new char[str[1].length()];
			
			for(int i=0;i<str[0].length();i++) {
				ch1[i]=str[0].charAt(i);
			}
			
			for(int i=0;i<str[1].length();i++) {
				ch2[i]=str[1].charAt(i);
			}
					
			for(int i=0;i<ch1.length;i++) {//第二个判定条件,借助共同数组
				if((ch1[i]>='a'&&ch1[i]<='z')||(ch1[i]>='A'&&ch1[i]<='Z')) {
					re1[0]=1;
				}else if(ch1[i]>='0'&&ch1[i]<='9'){
					re1[1]=1;
				}else {
					re1[2]=1;
				}
			}
			
			for(int i=0;i<ch2.length;i++) {
				if((ch2[i]>='a'&&ch2[i]<='z')||(ch2[i]>='A'&&ch2[i]<='Z')) {
					re2[0]=1;
				}else if(ch2[i]>='0'&&ch2[i]<='9'){
					re2[1]=1;
				}else {
					re2[2]=1;
				}
			}
			
			int count1=0;
			
			for(int i=0;i<3;i++){
				if(re1[i]==re2[i]) {		
					count1++;
				}	
			}
			if(count1==3) {
				System.out.println(count+1);
			}else {
			System.out.println(count);
			}			
		}
	}
}












![java八股文面试[多线程]——线程的生命周期](https://img-blog.csdnimg.cn/img_convert/1b2b2dfa02fa1ce4ec1c7972390a506f.png)





