Basic exercises string comparison of the Blue Bridge Cup VIP questions - JAVA

Basic exercises string comparison of the Blue Bridge Cup VIP questions - JAVA

Subject description
  given only by two strings uppercase or lowercase letters (a length between 1 and 10), the relationship between them is one of the following 4 conditions:
  1: two unequal length strings . For example Beijing and Hebei
  2: only two strings of equal length, but also the character at the corresponding position exactly (case sensitive), such as Beijing and Beijing
  . 3: two strings of equal length, only the character at a corresponding position are not distinguished under the premise of the case in order to achieve exactly the same (that is, it does not meet the case 2). For example beijing and beijing
  . 4: the length of the two strings are equal, but even these can not case sensitive nor two identical strings. Beijing and Nanjing such
  a relationship between two input strings programming determines which category of these four given class number belongs.
  
Input format
  includes two rows, each row is a string of
  
output format
  is only one digit, showing the relationship of these two numbering character string
  
Sample Input
beijing
beijing

Sample output
3


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	    String str_1 = sc.next();
	    String str_2 = sc.next();
	    
	    //校验长度!=  -- 校验是否完全相等equals  -- 校验是否是不区分大小写相等 equalsIgnoreCase
	    if(str_1.length() != str_2.length()){
	    	System.out.println(1);
	    }else{
	    	if(str_1.equals(str_2)){	    
	    		System.out.println(2);
	    	}else if(str_1.equalsIgnoreCase(str_2)){
	    		System.out.println(3);
	    	}else{	    	
	    		if(!str_1.equalsIgnoreCase(str_2)){
	    			System.out.println(4);
	    		}    	
	    	}
	    }
	}
}


There is also a writing, feeling more easier to understand:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	    String str_1 = sc.next();
	    String str_2 = sc.next();
	    int res = 1; 
	    
	    if(str_1.length()==str_2.length()){
	    	res=4;
	    }
	    
	  	if(str_1.length()!=str_2.length()){
	    	res=1;
	    }
	    
	    if(str_1.equalsIgnoreCase(str_2)){
	    	res=3;
	    }

	    if(str_1.equals(str_2)){
	    	res=2;
	    }

	    System.out.println(res);
	    
	}
}
Published 475 original articles · won praise 682 · views 520 000 +

Guess you like

Origin blog.csdn.net/Czhenya/article/details/104646663