-8
有人可以幫助我在初學者編程課程,我不能讓我的程序輸出字符串的最後一個字母?下面是我的程序:輸出字符串的最後一個字母
public class String {
private static java.lang.String string;
public static void main(java.lang.String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string: "); //user enters a word
java.lang.String word = input.nextLine();
System.out.print("The length of " + word + " is " +
word.length()); //gives the number of characters in the word
java.lang.String length = input.nextLine();
System.out.print("The first character: " + word.charAt(0)); //gives the first letter of word
java.lang.String first = input.next();
System.out.print("The last character: " + word.charAt(4)); //gives the last letter of word
java.lang.String last = input.next();
}
}
當然,因爲你得到的索引4,而不是最後一個字符。要獲得最後一個索引,string.length - 1 – Winter