2011-12-03 33 views
-5

我正在做一個hang子手項目。現在,我有一個TXT文件中的單詞列表。我有一個我需要使用的類RandomString。我正在研究Next方法並且卡住了。下面是我有:如何獲得數組列表的隨機單詞?

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Random; 
import java.util.Scanner; 

public class RandomString { 
    private String filename; 
    private ArrayList<String> phrases; 

    public RandomString(String filename) { 
     this.filename = filename; 
     reset(); 
    } 

    public void reset() { 
     phrases = new ArrayList<String>(); 

     try { 
      Scanner scan = new Scanner(new File(filename)); 
      while (scan.hasNext()) 
       phrases.add(scan.nextLine()); 
      scan.close(); 
     } 
     catch (Exception e){} 
    } 

    public String next() { 
     if (phrases.isEmpty()) 
      reset(); 
    } 
} 

我的下一個方法需要:看看該ArrayList是空的,如果是復位,然後得到介於0和列表的大小的隨機數,然後拿到項目,然後刪除該項目,然後返回該項目。

+2

你有什麼確切的問題?你不能得到一個隨機數字?無法獲得該物品?無法刪除該項目?無法退回? –

+2

您在這裏有很多問題,請嘗試縮小範圍 –

+1

您發佈的代碼是完全空的嗎?你不能指望在這裏填補空白的答案。如果是家庭作業,你需要努力解決你的問題。 – jman

回答

1

獲取隨機數:Math.Random投地int和乘以許多項目怎麼過您的列表和10

EG。在列表中

  • 6項,int abc=(int) (Math.random()*60);
  • 4項,int abc=(int) (Math.random()*40);

,但我不明白你的問題的其餘

,或者您可以使用java.util.random

+0

「隨機」類具有整數範圍隨機數的方法。也許他們也在做同樣的事情,但這只是減少錯誤的一個來源。 –

+0

我說math.random只是因爲我不能理解java.util.random !!!! :) – 2011-12-04 12:45:59

1

我的第一個意圖是評論你的編碼風格。這是不好的,所以如果有必要,特別是當從別人那裏詢問某些事情時,一定要特別留言

其他的東西是make/post something complete,例如。塊如if | else關閉和開頭的括號。

我想你需要從數組列表中根據內容隨機獲取項目。如果是這樣,你可以瞭解如何做到這一點。這段代碼將會處理你尚未完成的其他條件的內容。

這裏的答案爲您的問題,請遵循這裏使用的一些通用標準,使您的代碼更易於理解(無論您在哪裏寫 - 公司,帖子,博客等)。

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Random; 
import java.util.Scanner; 

public class RandomString { 
    private String filename; 
    private ArrayList<String> phrases; 
    // Random number generator instance 
    Random randomGenerator = new Random(); 

    public RandomString(String filename) 

    { 
     this.filename = filename; 
     reset(); 
    } 

    public void reset() { 
     phrases = new ArrayList<String>(); 

     try { 
      Scanner scan = new Scanner(new File(filename)); 
      while (scan.hasNext()) 
       phrases.add(scan.nextLine()); 
      scan.close(); 
     } catch (Exception e) { 
     } 
    } 

    public String next() { 

     // Value retrieved from array-list 
     String item = null; 
     // Index to be read from array-list 
     int index = 0; 

     // Reset the array-list if is it empty 
     if (phrases.isEmpty()) { 
      reset(); 
     } 

     // Check the size, there is a possibility to have zero elements in your stored file 
     if (phrases.size() > 0) { 

      if (phrases.size() > 1) { 
       // Get a random number 
       index = randomGenerator.nextInt(phrases.size()); 
      } else { 
       // If the array-list has only one item there is no need to get a random number. 
       index = 0; 
      } 
      // Get the indexed item 
      item = phrases.get(index); 
      // Remove item 
      phrases.remove(index); 

     } 

     // Return the item 
     return item; 
    } 
} 

所以盡你所能。