编写一个Java程序,将一个字符串重复100次,但在8个随机字符串中添加8个唯一的拼写错误



对在校儿童的常见惩罚是多次写出一个句子。编写一个Java独立程序,该程序将写出以下句子一百次:"我再也不会给我的朋友发垃圾邮件了。"你的程序应该对每一句话进行编号,并且应该做出八个看起来随意的拼写错误。

这就是我到目前为止所能做的。无法将其整合在一起。

public class Punishment {
public static void textgen(int x) {
System.out.println(x + ") I will never spam my friends again");
}
public static void main(String[] args) {
String a = "I will ever spam my friends again.";
String b = " will never spam my friends again.";
String c = "I will neer spam my friends again.";
String d = "I will never pam my friends again.";
String e = "I will never spam my riends again.";
String f = "I will never spam my friends gain.";
String g = "I will never spam y friends again.";
String h = "I will never sam my friends agn.";
String typos[] = {a,b,c,d,e,f,g,h};
int[] exitng = new int[8];
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i=1; i<=100; i++){
list.add(new Integer(i));
}
Collections.shuffle(list);
for (int i=0; i<8; i++){
exitng[i] = list.get(i);
System.out.println( list.get(i)+")- "+typos[i]);
}
for (int i = 1; i <= 100; i++) {
textgen(i);
}
}
}

这里有一个想法:

// initialize result without typos
String[] sentences = new String[100];
Arrays.fill(sentences, "I will never spam my friends again.");
// generate 8 unique typo indexes
Set<Integer> typoIndexes = new HashSet<>();
while (typoIndexes.size() < 8) {
typoIndexes.add(Math.random() * 100);
}
// create a typo at each typo index
for(int index : typoIndexes) { 
StringBuilder sb = new StringBuilder(sentences[index]);
char char1 = char2 = char1;
int pos1, pos2;
while (char1 != char2) {
pos1 = Math.random(sb.length());
char1 = sb.charAt(pos1);
pos2 = Math.random(sb.length());
char2 = sb.charAt(pos2);
}
sb.setCharAt(pos1, char2);
sb.setCharAt(pos2, char1);
sentences[index] = sb.toString();
}

注:未编译、未测试的

  • 将8个拼写错误的句子添加到数组或列表中
  • 添加适当句子的其余部分
  • 打乱整个数组或列表
  • 打印元素

更新

你应该实现一个函数,随机生成8个有错别字的句子,而不是预制它们,以便更好地与老师一起得分。


嗯,这有点像@Pschemo,但这里是:

public static void main(String[] args) {
String a = "I will ever spam my friends again.";
String b = " will never spam my friends again.";
String c = "I will neer spam my friends again.";
String d = "I will never pam my friends again.";
String e = "I will never spam my riends again.";
String f = "I will never spam my friends gain.";
String g = "I will never spam y friends again.";
String h = "I will never sam my friends agn.";
List<String> l = new ArrayList<>();
// Add the right sentence 92 time.
for (int i = 0; i < 92; i++) {
l.add("I will never spam my friends again.");
}
// Add 8 different typo sentences.
l.add(a);
l.add(b);
l.add(c);
l.add(d);
l.add(e);
l.add(f);
l.add(g);
l.add(h);
// Shuffle and print.
Collections.shuffle(l);
int i = 1;
for (String s: l) {
System.out.println(String.format("%d) %s", i++, s));
}
}

相关内容

最新更新