我正在尝试使用扫描仪创建一个程序,该程序从用户那里获取输入并确定输入是否以相同的字符开头和结尾。
import java.util.Scanner;
public class L7E6{
public static void main(String[]args){
String word;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type a word: ");
word = keyboard.nextLine();
if (word.charAt(0).equals(word.length()-1)){
System.out.println("The word "+word+" begins and ends with the character "+word.charAt(0));
}
else{
System.out.println("The word "+word+" begins with "+word.charAt(0)+" and ends with "+ (word.length()-1)+" these characters are not the same.");
}
}
}
我以前使用charAt(0)
and.length()-1
来确定第一个和最后一个字符,但它似乎在这里不起作用。