简体   繁体   中英

How does the following Java code operate?

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);
}

I am not understanding it, how can I understand this?

there are some places in your code that not met coding standards

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
}

Please find my inline comments:

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
    }

}
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);
}

See the Out put

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM