2016-08-04 106 views
0

好的,我正在編寫這個代碼來混合人文和STEM。我知道非常基本的Java代碼,所以我現在試圖堅持String方法。我知道使用數組可能會更容易,但我不太瞭解如何使用它們。到目前爲止,我已經編寫了計算字符串中單詞的代碼,以確定要刪除多少個單詞(其中一半)。接下來,我需要找出一種方法來隨機刪除一半的單詞並返回一個新的字符串,可能用空格替換已刪除的字母。我需要在java中隨機刪除一個字符串中的一半字

這裏是我到目前爲止的代碼:

public class wordcount 
{ 
    public static void main (String[] args) 
    { 

    System.out.println("Simple Java Word Count Program"); 

    String str1 = "Look, you want it you devour it and then, then good as it was you realize it wasn’t what you exactly wanted what you wanted exactly was wanting"; 

    String[] wordArray = str1.split("\\s+"); 
    int wordCount = wordArray.length; 

    System.out.println(str1 + ""); 

    System.out.println("Word count is = " + wordCount); 

    int wordCount2 = wordCount/2; 


} 

}

+0

你要問什麼問題嗎?你堅持如何產生隨機性或如何從你的陣列中刪除項目? – sprinter

+1

除了真正明顯的「什麼是你的問題」..請閱讀關於Java編碼風格指南。類名以大寫字母開頭,你也可以使用CamelCase ...甚至當你剛開始時:嘗試給你的東西提供有用的名字。就像:「str1」沒有說明預期的用途。將它重命名爲「fullSentence」之類的東西,突然之間很清楚它是什麼。並且不要調用一些字數組 - 收集類型可能會改變;所以就把它叫做「allTheWords」吧。 – GhostCat

回答

2

我的陣列複製到一個ArrayList,然後遍歷列表,並刪除隨機元素。我希望這是你正在尋找的答案類型。

public static void main(String[] args) { 

    String str1 = "Look, you want it you devour it and then, then good as it was you realize it wasn’t what you exactly wanted what you wanted exactly was wanting"; 

    String[] wordArray = str1.split("\\s+"); 
    ArrayList<String> wordList = new ArrayList<String>(Arrays.asList(wordArray)); 

    int wordCount = wordList.size(); 
    int halfWordCount = wordCount/2; 
    int tracker = 0; //counter for iterations in while loop 

    Random random = new Random(); 
    while(tracker < halfWordCount){ 
     int randomIndex = random.nextInt(wordList.size()); 
     wordList.remove(randomIndex); 
     tracker++; 
    } 
    System.out.println(wordList.toString()); 
} 
+0

我得到了一個工作結果,但我很喜歡輸入和修改。我還需要一種方法來爲詩歌提供輸入,並且只需在點擊鼠標後執行「刪除方法」 –

+0

我的回答是否適合您?如果是這樣,你能接受嗎? –

0
import java.util.Arrays; 
import java.util.* ; 
public class wordcount 
{ 
public ArrayList<Integer> test(Integer[] array) 
    { 
     ArrayList<Integer> list = new ArrayList<Integer>(); 
     for(int i=0; i<array.length; i++) 
      list.add(array[i]); 
     return list; 
    } 
public ArrayList<String> testS(String[] array) 
    { 
     ArrayList<String> list = new ArrayList<String>(); 
     for(int i=0; i<array.length; i++) 
      list.add(array[i]); 
     return list; 
    } 
public static void main (String[] args) 
{ 

    System.out.println("Removing random words in a Poem Program"); 

    String str1 = "Sample Poem by Noah Eli Gordon: Look, you want it you devour it and then, then good as it was you realize it wasn’t what you exactly wanted what you wanted exactly was wanting"; 

    String[] wordArray = str1.split("\\s+"); 
    int wordCount = wordArray.length; 

    System.out.println(str1 + ""); 

    //System.out.println("Word count is = " + wordCount); 

    //System.out.println(wordArray); 
    //String[] ret = wordArray; 
    //for(String str : ret) 
    // System.out.print(str); 

    int wordCount2 = wordCount/2; 

    Integer[] myIntArray = new Integer[wordCount]; 
    //for(int i = 0; i<wordCount;i++) 
    // myIntArray[i] = i; 
    //for(int str : myIntArray) 
     //System.out.print(str); 

    wordcount w = new wordcount(); 
    String[] wordArray2 = new String[wordCount2]; 

    for(int i = 0; i <= wordCount2; i++) 
    { 
     int rand = (int)(Math.random()*(myIntArray.length-1)); 
     ArrayList<Integer> list = w.test(myIntArray); 
     list.remove(rand); 
     myIntArray = list.toArray(myIntArray); 
     ArrayList<String> listS = w.testS(wordArray); 
     listS.remove(rand); 
     wordArray2 = listS.toArray(wordArray); 


    } 
    List<String> list = new ArrayList<String>(); 

    for(String s : wordArray2) 
    { 
     if(s != null && s.length() > 0) 
     { 
      list.add(s); 
     } 
    } 

    wordArray2 = list.toArray(new String[list.size()]); 

    //for(int str : myIntArray) 
     //System.out.println(str); 
    System.out.println(); 
    String[] ret2 = wordArray2; 
    for(String str : ret2) 
     System.out.print(str + " "); 
} 
} 
相關問題