我正在为一堂课的Pig Latin翻译工作。我之前有我的计划工作;它从translateWords数组列表中打印出底部for循环。但最新版本不会在底部打印循环。我怀疑它是一个大括号,但我似乎无法找到它。
package stringTest;
import java.util.ArrayList;
public class StringTest {
public static void main(String[] args) {
String userString = "THIS IS A STRING";
// We might need to trim the string first it throws an error if theres
// white space around word
// Making string lowercase, first
userString = userString.toLowerCase();
// Splitting up string into individual words
String[] stringArray = userString.split(" ");
ArrayList<String> translatedWords = new ArrayList<String>();
// going through each string with foreach loop
for (String ss : stringArray) {
System.out.println("prints here in intial for loop");
// pulling out the words that start with a vowel
// since they just get "way" at the end
if (ss.charAt(0) == 'a' || ss.charAt(0) == 'e'
|| ss.charAt(0) == 'i' || ss.charAt(0) == 'o'
|| ss.charAt(0) == 'u') {
ss = ss.concat("way");
translatedWords.add(ss);
}
// If the words don't start with a vowel
// trying to figure out how to cut them at first vowel and
// concatenate to end
else {
for (int i = 0; i < ss.length();) {
if (ss.charAt(i) == 'a' || ss.charAt(i) == 'e'
|| ss.charAt(i) == 'i' || ss.charAt(i) == 'o'
|| ss.charAt(i) == 'u') {
ss = ss.substring(i, ss.length());
String sss = ss.substring(0, i + 1);
String ss44 = ss.substring(i + 1);
String ss33 = ss44 + sss;
ss33 = ss33 + "ay";
translatedWords.add(ss33);
System.out.println(ss33);
System.out.println("Why won't this print");
break;
}
}
}
for (String fs : translatedWords) {
System.out.print(fs.toString() + " ");
}
}
}
}
答案 0 :(得分:2)
它不是一个大括号,但for循环无限运行
i
永远不会在for语句中或在for
因此if条件将继续为字符串的第一个字符运行,如果它不是字符串的假元素,那么它将永远不会进入if语句
//for (int i = 0; i < ss.length();) {//no i++ implemented
for (int i = 0; i < ss.length();i++) {
if (ss.charAt(i) == 'a' || ss.charAt(i) == 'e'
|| ss.charAt(i) == 'i' || ss.charAt(i) == 'o'
|| ss.charAt(i) == 'u') {
ss = ss.substring(i, ss.length());
String sss = ss.substring(0, i + 1);
String ss44 = ss.substring(i + 1);
String ss33 = ss44 + sss;
ss33 = ss33 + "ay";
translatedWords.add(ss33);
System.out.println(ss33);
System.out.println("Why won't this print");
break;
}
}