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

Java 检查第一个字符是否等于最后一个字符,java,Java,我对java相当陌生,我想知道如果第一个字母和最后一个字母不一样,如何将代码返回false 整个指令是定义和测试一个名为checkString的方法,该方法将把单词作为参数,并检查字符串是否以相同的字母开头和结尾。如果两个字母相同,则该方法返回true,否则返回false(返回布尔值)。该程序将大写字母和小写字母视为等效字母 以下是我所拥有的: import java.util.Scanner; public class Excercise5 { public s

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

整个指令是定义和测试一个名为checkString的方法,该方法将把单词作为参数,并检查字符串是否以相同的字母开头和结尾。如果两个字母相同,则该方法返回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;
            }
        }
    }

按照你的代码;而不是:

if (letter1.equals(lastletter)) {
  return true;
} else {
  return false;
}
System.out.printf (word + " begins and ends with the same letter.", checkString(word));
只要做:

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;
}
也不是:

if (letter1.equals(lastletter)) {
  return true;
} else {
  return false;
}
System.out.printf (word + " begins and ends with the same letter.", checkString(word));
使用
print()

编辑:

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

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

%s
就像是
word
的占位符,以及
checkString(word)
根据您的代码返回的值;而不是:

if (letter1.equals(lastletter)) {
  return true;
} else {
  return false;
}
System.out.printf (word + " begins and ends with the same letter.", checkString(word));
只要做:

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;
}
也不是:

if (letter1.equals(lastletter)) {
  return true;
} else {
  return false;
}
System.out.printf (word + " begins and ends with the same letter.", checkString(word));
使用
print()

编辑:

如果要使用
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;
}

如果你想把它排成一行:

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 boolean firstAndLast(String word)
{
    return Character.toUpperCase(word.charAt(0)) == Character.toUpperCase(word.charAt(word.length()-1));
}

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

您可以执行以下操作:

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

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

您不需要如此复杂的代码

这里有一个非常简单的书面方法,可以完美地完成工作


密码
解释
它将字符串输入的第一个和最后一个字符比较为小写,以便匹配小写和大写。

您不需要如此复杂的代码

这里有一个非常简单的书面方法,可以完美地完成工作


密码
解释
它将字符串输入的第一个和最后一个字符比较为小写,以便匹配小写和大写。

您使用printf是错误的,因为您没有打印出真/假值。将布尔说明符添加到输出字符串或使用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));

使用printf是错误的,因为您没有打印出真/假值。将布尔说明符添加到输出字符串或使用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;
        }
    }

这将把字符串转换成小写。(您也可以通过
word.toUpperCase();
来实现这一点)之后,将字符串转换为字符数组,然后检查第一个和最后一个“字符”。如果给定字符串的长度为“0”,则将返回
true
。根据您想要如何决定emtpy字符串,您还可以将其更改为
false

,您可以执行以下操作

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

这将把字符串转换成小写。(您也可以通过
word.toUpperCase();
来实现这一点)之后,将字符串转换为字符数组,然后检查第一个和最后一个“字符”。如果给定字符串的长度为“0”,则将返回
true
。根据您想要如何决定emtpy字符串,您还可以将其更改为
false

,这将为您提供所需的内容

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

这将获取最后一个字母:
firstletter=(word.charAt(0)).toUpperCase()最后一个字符位于索引
word.length-1
。例如,如果您的单词是“apple”,则长度为5,但最后一个字符位于索引4处(请记住,0-4不是1-5;字母1具有误导性,实际上是字母0)

正如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())

这将为您提供所需的信息

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

这将获取最后一个字母:
firstletter=(word.charAt(0)).toUpperCase()最后一个字符位于索引
word.length-1
。例如,如果您的单词是“apple”,则长度为5,但最后一个字符位于索引4处(请记住,0-4不是1-5;字母1具有误导性,实际上是字母0)

正如Savior Self所提到的,使用
charAt
更有效。此外,您还可以使用
char
,而不是
String
,以及
=
来比较