2016-05-23 48 views
-3

對學齡兒童的一個常見處罰是多次寫出一個句子。編寫一個Java獨立程序,它會將下面的句子寫出一百遍:「我永遠不會再次給我的朋友發送垃圾郵件。」你的程序應該爲每個句子編號,它應該做八個不同的隨機錯字。編寫一個Java程序重複一次字符串100次,但隨機添加8個唯一的拼寫錯誤字符串

這就是我到目前爲止所能做到的。無法一起去。

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(); 
} 

注:未編譯的,未經檢驗

+1

'學齡兒童常見的懲罰是一個句子多次寫出來的。編寫一個Java獨立程序,它會將下面的句子寫出一百次:「我永遠不會再給我的朋友發垃圾郵件」。你的程序應該爲每個句子編號,它應該做八個不同的隨機拼寫錯誤「這是你的作業副本/粘貼?看起來像。 「這是我迄今能夠做到的。無法一起去。「?怎麼了 ?有什麼問題?編輯您的文章並向其發佈問題。 – UDKOX

+0

將隨機錯字插入到正確的句子中比預製錯誤的8個句子要好得多 –

+0

不要問你在stackoverflow上的作業。 –

回答

2
  • 有錯別字增加8句到您的數組或列表
  • 添加正確的句子的其餘
  • 洗牌整個陣列或列表
  • 個打印元件
1

UPDATE

您應該實現的功能,產生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)); 
    } 
} 
+0

謝謝。你真棒:) – VIP

+0

因爲我解決了你的任務而給了我一個downvote:P – totoro

+0

* upvoted因爲不需要downvote * – Kaelinator

相關問題