以下Java代码如何运行?

时间:2019-03-21 09:34:42

标签: java string

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 个答案:

答案 0 :(得分:0)

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

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
}

答案 1 :(得分:0)

请找到我的内联评论:

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
    }

}

答案 2 :(得分:0)

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