當我嘗試重複代碼時,出現此錯誤。螺紋當用戶輸入重複代碼時,出現錯誤
異常「主要」 java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:1種 類型的句子,並將該軟件會告訴你 多少元音也有(不包括「Y」): 是java。 lang.String.substring(String.java:1934) 在countvowels.Main.main(Main.java:53) Java結果:1
這裏是我的代碼:
Scanner input = new Scanner(System.in);
int answer = 0;
System.out.println("Count Vowels \n============");
// the do-while loop makes sure that the code is executed at least once
do {
if(answer == 1) {
System.out.println("You have chosen to count the vowels in another phrase");
}
System.out.println("Type a sentence and this program will tell you\nhow many vowels there are (excluding 'y'):");
String string1;
string1 = input.nextLine();
string1 = string1.toLowerCase();
int vowels=0;
int i = 0;
for (String Vowels : string1.split(" ")) {
for (i = 0; i < Vowels.length(); i++) {
int letter = Vowels.charAt(i);
if (letter == 'a' || letter == 'e' || letter == 'i'
|| letter == 'o' || letter == 'u') {
vowels++;
}
}
System.out.println(Vowels.substring(0,1)
+ Vowels.substring(1) + " has " + vowels + " vowels");
vowels = 0;
}
System.out.println("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press 2");
} while(input.nextInt() == 1);
System.out.println("Have a nice day");
}
}
當您通過本在調試步驟,是什麼導致錯誤的字符串的運行時間值? – David 2014-11-14 16:04:40