【问题标题】:How to get random words for Array List?如何获取数组列表的随机单词?
【发布时间】:2011-12-03 06:02:49
【问题描述】:

我正在做一个刽子手项目。目前,我有一个 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 和列表大小之间的随机数,然后获取项目,然后删除项目,然后返回项目.

【问题讨论】:

  • 您有什么具体问题?你不能得到一个随机数?无法获得该项目?无法删除该项目?不能退货?
  • 这里有很多问题,尽量缩小范围
  • 您接下来发布的代码完全是空的?你不能指望在这里填空答案。你需要付出一些努力来解决你的问题尤其是如果它是家庭作业的话。
  • 这里没什么好说的,我可以建议向我们展示更多您在Next() 方法中尝试的内容吗?

标签: java arraylist


【解决方案1】:

我的第一个意图是评论您的编码风格。这很糟糕,所以如果有必要,请务必在向别人询问时特别放置 cmets。

其他事情是制作/发布完整的东西,例如。像 if | 这样的块带有右括号和左括号的 else 语句。

我猜您需要根据其内容从数组列表中随机获取项目。如果是这样,您可以了解如何做到这一点。此代码将转到您未完成的 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;
    }
}

所以要尽力而为。

【讨论】:

    【解决方案2】:

    获取随机数:Math.Random 转换为 int 并乘以列表中的项目数和 10。

    例如。

    • 列表中有 6 项,int abc=(int) (Math.random()*60);
    • 4 项,int abc=(int) (Math.random()*40);

    但我不明白你的其余问题

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

    【讨论】:

    • Random 类具有整数范围随机数的方法。可能他们也在做同样的事情,但这只是少了一个错误来源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多