第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

線程“main”中的異常 java.lang.StringIndexOutOfBounds

線程“main”中的異常 java.lang.StringIndexOutOfBounds

精慕HU 2021-11-03 11:00:21
我正在嘗試打印姓氏字符,但我的代碼生成了異常。代碼:import java.util.Scanner;public class LastCharacter {    public static void main(String[] args) {        Scanner reader = new Scanner(System.in);        System.out.println("Type your name: ");        String name = reader.nextLine();        char nameLastChar = lastCharacter(name);        System.out.println("Last character = " + nameLastChar);    }    public static char lastCharacter(String text){        int last = text.length();        char lastChar = text.charAt(last);        return lastChar;    }}例外:線程“main”中的異常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范圍:7我找不到我的錯誤,我不明白異常信息。
查看完整描述

1 回答

?
largeQ

TA貢獻(xiàn)2039條經(jīng)驗 獲得超8個贊

如果字符串長度為 7 個字符,則最后一個索引是 6,而不是 7。請記住,索引從 0 開始。


你要


int last = text.length() - 1; // Adjust the index

char lastChar = text.charAt(last);


查看完整回答
反對 回復(fù) 2021-11-03
  • 1 回答
  • 0 關(guān)注
  • 206 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號