HangMan RandomString类 [英] HangMan RandomString Class

查看:86
本文介绍了HangMan RandomString类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Java RandomString类


以下是方向:



创建一个 RandomString 类,并执行以下操作:


  1. 创建一个名为guess_phrases.txt的文件,其中包含您的Hangman游戏中要猜到的短语。该文件每行将有一个猜测短语。


  2. 一个构造函数,它接收文件的名称以获取字符串值。构造函数应该从文件中读取短语并存储以备以后使用。


  3. 从文件返回随机字符串值的方法;这个值不应该重复,直到文件中的所有猜测短语都被使用为止。


创建一个主要方法测试下一个工作是否正确通过重复调用next&打印结果 - 您不应该有任何重复,短语不应该与文件中的顺序相同。



我创建了一个名为的文件guess_phrases带有随机短语的.txt 当我运行这个我收到一个错误,也不是随机打印,为什么这样?如何解决这个问题?



错误
线程main中的异常java.lang.IllegalArgumentException:n必须为正值

 在java.util.Random.nextInt(未知来源)
在RandomString.next(RandomString.java:32)
在RandomString。 main(RandomString.java:40)

这是我在RandomString类中有的

  public class RandomString {

随机随机= new Random();
ArrayList< String> guessPhrases = new ArrayList< String>();
扫描仪fileScan;

public RandomString(String guessPhrases)throws FileNotFoundException {

//创建一个Scanner对象从文件中读取
fileScan = new Scanner(new File(guess_phrases 。文本));

//将所有短语从文件添加到ArrayList
while(fileScan.hasNext()){

String line = guessPhrases.nextLine() ; // get input
System.out.println(line); // print line
guessPhrases.add(line); //添加行到数组列表
}
}

public String next(){
int i = random.nextInt(guessPhrases.size());
return guessPhrases.get(i);
}

public static void main(String [] args){

}
}
/ pre>

解决方案

随机API



你可以看到,如果你将一个negetive数字传递给nextInt(pos),它会抛出一个 IllegalArgumentException



int i = random。 nextInt(guessPhrases.size());



绝对 guessPhrases.size() / strong>,因此例外


Possible Duplicate:
Java RandomString Class

Here are the directions:

Create a RandomString class and implement the following:

  1. Create a file named guess_phrases.txt that contains phrases to be guessed in your Hangman game. This file will have one guess phrase per line.

  2. A constructor that receives the name of a file to get string values from. The constructor should read in the phrases from the file and store them for later use.

  3. A method that returns a random string value from the file; this value shouldn't be repeated until all guess phrases in the file have been used.

Create a main method to test that next is working correctly by repeatedly calling next & printing the result – you should not have any repeats, and the phrases should not be in the same order as in the file.

I created a file called guess_phrases.txt with random phrases. When I run this I get an error, also it is not printed randomly, why is this? How can I fix this ?

error Exception in thread "main" java.lang.IllegalArgumentException: n must be positive

at java.util.Random.nextInt(Unknown Source)
at RandomString.next(RandomString.java:32)
at RandomString.main(RandomString.java:40)

This is what I have in the RandomString Class

public class RandomString {

    Random random = new Random();
    ArrayList<String> guessPhrases = new ArrayList<String>();
    Scanner fileScan;

    public RandomString(String guessPhrases) throws FileNotFoundException {

        // create a Scanner object to read from the file
        fileScan = new Scanner(new File("guess_phrases.txt"));

        // add all of the phrases from the file into the ArrayList
        while (fileScan.hasNext()) {

            String line = guessPhrases.nextLine(); // get input
            System.out.println(line); // print line
            guessPhrases.add(line); // add line to array list
        }
    }

    public String next() {
        int i = random.nextInt(guessPhrases.size());
        return guessPhrases.get(i);
    }

    public static void main(String[] args) {

    }
}

解决方案

From Random API

you can see that if you pass a negetive number or zero to nextInt(pos) it will throw an IllegalArgumentException

        int i = random.nextInt(guessPhrases.size());

Most definitely guessPhrases.size() is zero, thus the Exception

这篇关于HangMan RandomString类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆