2 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
你的代碼絕對(duì)不是Java語言。
第一的:
if (int startIndex > inputSting.length) { .. }
您不能在if
語句內(nèi)或比較表達(dá)式中聲明變量,而且該變量startIndex
已經(jīng)存在。
第二:
inputSting
變量不存在并且它肯定沒有屬性length
。inputString
可能是+ string 有方法length()
而不是屬性的拼寫錯(cuò)誤。

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊
更改以下內(nèi)容:
int startIndex到startIndex,
int endIndex到endIndex,
inputSting.length到inputString.length()
所以固定代碼,
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputString="";
int startIndex;
int endIndex;
System.out.println("Enter a string : ");
inputString = scanner.nextLine();
System.out.println("Enter the first index of the substring : ");
startIndex = scanner.nextInt();
if ( startIndex > inputString.length()) {
System.out.println("Index is not in string length, try again.");
}
System.out.println("Enter the second index of the substring : ");
endIndex = scanner.nextInt();
if ( endIndex > inputString.length()) {
System.out.println("Index is not in string length, try again.");
}
char[] ch = new char[endIndex - startIndex + 1];
inputString.getChars(startIndex, endIndex + 1, ch, 0);
System.out.println("Output : " + String.valueOf(ch));
}
}
添加回答
舉報(bào)