2 回答

TA贡献1831条经验 获得超9个赞
你的代码绝对不是Java语言。
第一的:
if (int startIndex > inputSting.length) { .. }
您不能在if
语句内或比较表达式中声明变量,而且该变量startIndex
已经存在。
第二:
inputSting
变量不存在并且它肯定没有属性length
。inputString
可能是+ string 有方法length()
而不是属性的拼写错误。

TA贡献1786条经验 获得超13个赞
更改以下内容:
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));
}
}
添加回答
举报