繁体   English   中英

检查第一个字符是否等于最后一个字符

[英]checking if first char is equal to last char

我对Java相当陌生,我想知道如果第一个字母和最后一个字母不同,如何将我的代码返回为false

整个指令是定义并测试一个名为checkString的方法,该方法将单词作为参数并检查String是否以相同的字母开头和结尾。 如果两个字母相同,则该方法返回true,否则返回false(返回一个布尔值)。 该程序将大小写字母等效

这是我所拥有的:

    import java.util.Scanner;
    public class Excercise5 {
        public static void main(String[] arg) {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Type a string: ");
            String word = keyboard.nextLine(); 
            System.out.printf ("%s begins and ends with the same letter.", checkString(word));
        }
        public static boolean checkString (String word) {   
            int stringLength = word.length();
            String letter1 = (word.substring (0,1)).toUpperCase();
            String lastletter = (word.substring ((stringLength-1),(stringLength))).toUpperCase();

            if (letter1.equals(lastletter)){
                return true;
            } else {
                return false;
            }
        }
    }

您可以执行以下操作:

public boolean firstAndLast(String word)
{
    return Character.toUpperCase(word.charAt(0)) == Character.toUpperCase(word.charAt(word.length()-1));
}

这将检查0和length-1的位置,以查看它们是否相等。 如果是,则返回true,否则返回false。

根据您的代码; 代替:

if (letter1.equals(lastletter)) {
  return true;
} else {
  return false;
}

做就是了:

return letter1.equals(lastletter);

但是,您的checkString() {...}代码应为:

public static boolean checkString (String word) {
  int len = word.length();
  word = word.toUpperCase(); //since you are testing them as upper case
  char firstLetter = word.charAt(0);
  char lastLetter = word.charAt(len - 1);
  return firstLetter == lastLetter;
}

也可以代替:

System.out.printf (word + " begins and ends with the same letter.", checkString(word));

使用print()

System.out.print(word + " begins and ends with the same letter: " + checkString(word));

编辑:

如果要使用printf()尝试以下操作:

System.out.printf("%s begins and ends with the same letter. %s", word , checkString(word));

%s就像word的占位符,是checkString(word)返回的值。

如果要单行处理:

return (word.substring (0,1)).toUpperCase().equals(word.substring(word.length()-1).toUpperCase());

.equals(...)之前,它获取第一个字符,将其转换为一个大小写,只要以后使用相同的大小写就无关紧要。

word.substring(string.length()-1).toUpperCase();

获取最后一个密钥并将其转换为大写。

这就是我最可能写的方式

private boolean isFirstAndLastEqual (String word) {
    char first = Character.toUpperCase(word.charAt(0));
    char last  = Character.toUpperCase(word.charAt(word.length()-1));

    return first == last;
}

您不需要如此复杂的代码。

这是一个非常简单的书面方法,可以很好地完成工作。


public static boolean checkString (String word) {
    return word.toLowerCase().charAt(0) == word.toLowerCase().charAt(word.length()-1);
}

讲解

它比较输入到小写字母的字符串输入的第一个和最后一个字符,以便与小写和大写字母匹配。

您使用printf是错误的,因为您没有打印出true / false值。 在输出字符串中添加布尔说明符,或者使用println代替。

    System.out.printf (word + " begins and ends with the same letter - %b\n", checkString(word));

    System.out.println (word + " begins and ends with the same letter " +  checkString(word));

您可以执行以下操作

public static boolean checkString(final String word) {
        final String checkedString = word.toLowerCase();
        final char[] chararray = checkedString.toCharArray();
        if (chararray.length > 1) {
            return chararray[0] == chararray[chararray.length-1];
        } else {
            return true;
        }
    }

这会将String转换为小写。 (您也可以使用word.toUpperCase();实现word.toUpperCase(); )之后,将String转换为char Array,然后检查第一个和最后一个“ char”。 如果给定的字符串长度为“ 0”,则将返回true 根据您想要确定空字符串的方式,也可以将其更改为false

这应该给您您想要的东西。

这将获取第一个字母(索引为0的字符): firstletter = (word.charAt(0)).toUpperCase();

这得到最后一个字母: firstletter = (word.charAt(0)).toUpperCase(); 最后一个字符在索引word.length-1 例如,如果您的单词是“ apple”,则长度为5,但最后一个字符在索引4(请记住,0-4而不是1-5; letter1会误导您,实际上是letter0)。

正如Savior Self提到的那样,使用charAt效率更高。 同样,您将使用char而不是String==进行比较。

同样,当您调用checkString时,可以将返回值分配给变量。 例如, boolean checkStr = checkString(word); 然后在您的打印语句中使用checkStr

import java.util.Scanner;
        public class Excercise5 {
            public static void main(String[] arg) {
                Scanner keyboard = new Scanner(System.in);
                System.out.print("Type a string: ");
                String word = keyboard.nextLine(); 
                boolean checkStr = checkString(word);
                System.out.println(word + " begins and ends with the same letter: " + checkStr;
            }
            public static boolean checkString (String word) {
                char firstletter = (word.charAt(0)).toUpperCase();
                char lastletter = (word.charAt(word.length-1)).toUpperCase();
                return (firstletter == lastletter);
            }
        }

另外,由于我没有足够的意见要发表,所以更好的单行代码是:

return (word.charAt(0)).toUpperCase() == (word.charAt(word.length-1)).toUpperCase());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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