gpt4 book ai didi

Java:方法接受一个字符串并构造一个程序,使用 for 循环输出相同的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 14:45:08 25 4
gpt4 key购买 nike

这是一个实验。我的 friend 正在尝试掌握 java 并编写文本输出,如下例所示。我想测试一下自己是否可以提供一个可以生成它们的程序。

对于字符串,程序应该计算每个字符并保存它出现的频率和索引,然后为每个字符生成一个带有 if 子句的 for 子句,如下所示:

我想输入例如“1234123412341234”并得到类似的东西

public class ClauseText {
public static void main(String[] args) {
for(int i = 0; i < 16; i++) {
if (i == 0 || i == 4 || i == 8 || i == 12) {
System.out.print("a");
}
if (i == 1 || i == 5 || i == 9 || i == 13) {
System.out.print("b");
}
if (i == 2 || i == 6 || i == 10 || i == 14) {
System.out.print("c");
}
if (i == 3 || i == 7 || i == 11 || i == 15) {
System.out.print("d");
}
}
}
}

到目前为止,我想到的内容如下。我将输入字符串转换为字符数组并迭代该数组。我维护三个数组列表,一个用于字符,一个用于它出现的频率,一个保存包含字符出现的索引位置的整数 ArrayList。

为了更简单,我决定将所有 ArrayList 的大小设置为 128,并将每个字符放入等于其各自 ASCII 值的索引。

但它似乎不起作用,这是一个简单的 IndexoutofboundsException,但是,我不知道出了什么问题。这里:

输出仅到此为止,然后就崩溃了:

public class ClauseText {
public static void main(String[] args) {
for(int i = 0; i < 6; i++) {
if (i == 0 || i == 3) {
System.out.print("a");
}
if (i == 1 || i == 4) {
System.out.print("b");
}
if (i == 2 || i == 5

程序是:

import java.util.ArrayList;

public class StringToProgram {

public static void main(String[] args) {
// define and create program
String className = "ClauseText";
String program = makeProgram("abcabc", className);
}

public static String makeProgram(String myWord, String className) {
String program = "public class " + className + " {\n";
program += " public static void main(String[] args) {\n";
program += " for(int i = 0; i < " + myWord.length()
+ "; i++) {\n";
char[] myWordChar = myWord.toCharArray();

// For each character, we have to save the index where it occurs and how
// often. We want to hash it into the ArrayLists by ASCII value.
ArrayList<Character> characters = new ArrayList<Character>();
ArrayList<ArrayList<Integer>> indices = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> amounts = new ArrayList<Integer>();

// Initialize three lists with the size of the ASCII table
for (int i = 0; i < 128; i++) {
characters.add(null);
indices.add(new ArrayList<Integer>());
amounts.add(0);
}

// Now we iterate through each occurrence. We use the ASCII code to hash
// and find specific letters.
for (int i = 0; i < myWordChar.length; i++) {

int index = (int) myWordChar[i]; // the hash value of the char

if (amounts.get(index).equals(0)) {
// create new entries and append them to the
// given index of the lists
characters.add(index, myWordChar[i]);
indices.add(index, new ArrayList<Integer>());
indices.get(index).add((Integer) i);
amounts.add(index, 1);
} else {
// there is already an entry. modify it.
amounts.add(index, amounts.get(index) + 1); // ++
indices.get(index).add((Integer) i);
}
}

// Now, we iterate through the occurrences list. First, we check for
// each index if an object is saved there.
for (int i = 0; i < amounts.size(); i++) {
if (amounts.get(i) > 0) {
// When this is the case, we append an if clause.
program += " if (i == ";
for (int j = 0; j < amounts.get(i); j++) {
// The amount of options in the if clause depends on how
// often the character occurred in the string.
program += indices.get(i).get(j);
if (j + 1 < amounts.get(i)) {
// we still have to append an option
program += " || i == ";
}
}
program += ") {\n";
program += " System.out.print(\""
+ characters.get(i) + "\");\n";
program += " }\n";
}
}
program += " }\n";
program += " }\n";
program += "}";

System.out.println(program);
return program;
}

}

错误如下:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at StringToProgram.makeProgram(StringToProgram.java:60)
at StringToProgram.main(StringToProgram.java:8)

最佳答案

也许你应该从更简单的开始:

public class StringToProgram {

public static void main(String[] args) {
// define and create program
String className = "ClauseText";
String program = makeProgram("a\nbca\rbc", className);
}

public static String makeProgram(String myWord, String className) {
myWord = myWord.replace("\\", "\\\\");
myWord = myWord.replace("\t", "\\t");
myWord = myWord.replace("\b", "\\b");
myWord = myWord.replace("\n", "\\n");
myWord = myWord.replace("\r", "\\r");
myWord = myWord.replace("\f", "\\f");
myWord = myWord.replace("\'", "\\\'");
myWord = myWord.replace("\"", "\\\"");
myWord = myWord.replace("\t", "\\t");
String program = "public class " + className + " {";
program += " public static void main(String[] args) {";
program += " System.out.println(\"" + myWord + "\");";
program += " }";
program += "}";
System.out.println(program);
return program;
}

}

为了清楚起见,我没有使用 org.apache.commons.lang.StringEscapeUtils。

关于Java:方法接受一个字符串并构造一个程序,使用 for 循环输出相同的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493233/

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