write a class P2 which has a main method and an isLonger method.The isLonger method -is static -takes two Strings as parameters-returns a boolean-use an appropiate method of the String class to find the length of each String-returns true if the first

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/29 17:33:33

write a class P2 which has a main method and an isLonger method.The isLonger method -is static -takes two Strings as parameters-returns a boolean-use an appropiate method of the String class to find the length of each String-returns true if the first
write a class P2 which has a main method and an isLonger method.The isLonger method
-is static
-takes two Strings as parameters
-returns a boolean
-use an appropiate method of the String class to find the length of each String
-returns true if the first String is longer than the second
-returns false if the first String is not longer than the second
In the main method get user input for two Strings called first and second.Use the static isLonger method to find if the first is longer than the second,and print a message to state whether it is longer or not.

write a class P2 which has a main method and an isLonger method.The isLonger method -is static -takes two Strings as parameters-returns a boolean-use an appropiate method of the String class to find the length of each String-returns true if the first

编写一个类P2其中有一个主要的方法和isLonger方法.该isLonger方法 

- 是静态的 

- 接受两个字符串作为参数 

- 返回一个布尔值 

- 使用String类的方法来找到每个字符串的长度 

如果第一个字符串大于第二个返回true 

如果第一个字符串不超过第二个,返回false 

在main方法中输入两个字符串.使用静态isLonger方法,并且打印一个消息输入第一个是否比第二个长.



public class P2 {
    public static boolean isLonger(String str1,String str2){
        if(str1.length() > str2.length()){
            return true;
        }else{
            return false;
        }
    }
    
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一个字符串");
        String str1 = sc.next();
        System.out.println("请输入第二个字符串");
        String str2 = sc.next();
        boolean bReturn = isLonger(str1,str2);
        if(bReturn){
            System.out.println("第一个字符串比第二个字符串长");
        }else{
            System.out.println("第一个字符串比第二个字符串短");
        }
    }
}