gpt4 book ai didi

java - 我在 Eclipse 上的 Java 程序停止打印到控制台,但之前它工作正常

转载 作者:行者123 更新时间:2023-11-30 03:21:24 26 4
gpt4 key购买 nike

我正在为一个类(class)开发 Pig Latin 翻译器。我的程序之前已经运行过;它在底部的 for 循环中从translatedWords 数组列表中打印出来。但最新版本不会在底部 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() + " ");
}

}
}
}

最佳答案

这不是花括号,而是 for 循环无限运行

i 在 for 语句中或 for 内部永远不会递增

因此,if 条件将继续运行字符串的第一个字符,如果它不是元音,则字符串“this”将为 false,因此它永远不会进入 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;

}
}

关于java - 我在 Eclipse 上的 Java 程序停止打印到控制台,但之前它工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31235098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com