有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

string下面的Java代码是如何操作的?

public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        String B=sc.next();
        System.out.println(A.length()+B.length());
        System.out.println(A.compareTo(B)>0?"Yes":"No");
        System.out.println(capitalizeFirstLetter(A) + " " + capitalizeFirstLetter(B));
    }

    public static String capitalizeFirstLetter(String original) {
    if (original == null || original.length() == 0) {
        return original;
    }
    return original.substring(0, 1).toUpperCase() + original.substring(1);
}

我不明白,我怎么能明白呢


共 (3) 个答案

  1. # 1 楼答案

    public static void main(String[] args) { 
    
        Scanner sc=new Scanner(System.in); //create Scanner Object,use Scanner objects in order to get input from the user
        String A=sc.next();//first user input
        String B=sc.next();//second user input
        System.out.println(A.length()+B.length());//add the length of first and second user input and print
        System.out.println(A.compareTo(B)>0?"Yes":"No");// check condition if A>B then "Yes" Else "No" and print result
        System.out.println(capitalizeFirstLetter(A) + " " + capitalizeFirstLetter(B));//call the capitalizeFirstLetter() method and print result
    }
    
    public static String capitalizeFirstLetter(String original) {
    if (original == null || original.length() == 0) {
        return original;
    }
      return original.substring(0, 1).toUpperCase() + original.substring(1);
    }
    

    看到了吗

    enter image description here

  2. # 2 楼答案

    请查看我的在线评论:

    import java.util.Scanner;
    
    public class stack {
    
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in); //Scanner is for taking the user input
            String A = sc.next();                //For Example: A=pandey
            String B = sc.next();                //For Example: B=nishanth
            System.out.println(A.length() + B.length());//Prints the length of String A +String B, So Total will be 14
            System.out.println(A.compareTo(B) > 0 ? "Yes" : "No");//This line compares the string A and String B,If String A >String B then it prints Yes. if A<B then it prints No. If String length A=B, it print No
            System.out.println(capitalizeFirstLetter(A) + " " + capitalizeFirstLetter(B));//From here the control is pass to line no. 15. Final Output: Pandey Nishanth
        }
    
        public static String capitalizeFirstLetter(String original) {
            if (original == null || original.length() == 0) { //Here you are comparing your input to null or ==0 
                return original; //If the condition is true it will return the original string
            }
            return original.substring(0, 1).toUpperCase() + original.substring(1);//if condition is not true, it will return first letter of String A to "P" and first letter of String B to "N".  And the control will pass to line no. 12
        }
    
    }
    
  3. # 3 楼答案

    代码中有些地方不符合编码标准

    public static void main(String[] args) {
    
            Scanner sc=new Scanner(System.in);// sc object help you to input something using key board 
            String A=sc.next();//you can type any string and that string will hold by  variable A
            String B=sc.next();//you can type any string and that string will hold by variable B
            System.out.println(A.length()+B.length());// print the sum length of two variables(how many characteristics in both strings)
            System.out.println(A.compareTo(B)>0?"Yes":"No");// this line compare the two string and if two strings are equal answer is 0. 
            System.out.println(capitalizeFirstLetter(A) + " " + capitalizeFirstLetter(B));// this line print the two strings with capital first letters
        }
    
        public static String capitalizeFirstLetter(String original) {
        if (original == null || original.length() == 0) {
            return original;//return original string if above condition is true. || means or condition
        }
        return original.substring(0, 1).toUpperCase() + original.substring(1);//if condition is not true return the string value. if string is cat return CAa
    }